swift - 如何在 SwiftUI 中检查 json 数据的键是否存在?
问题描述
解决方案
Make subtitle
optional in model and use it conditionally in view, like
struct Post: Codable, Hashable{
var id: Int
var title: String
var subtitle: String? // << this !!
var body: String
}
and somewhere in body
:
VStack(alignment: .leading) {
Text(data[index].title)
if let subtitle = data[index].subtitle {
Text(subtitle)
}
}
推荐阅读
- php - laravel 5.5 全局作用域被删除
- javascript - 在 JavaScript 中构建大字符串;加入方法最有效吗?
- ios - UIStackView 和填充分布 - 不尊重内容拥抱优先级
- python - 如何使 pyqt gui 刷新
- sql - SQL Server:按年份汇总行并将行值命名为当前年份的“当前”和去年的“上一个”
- python - 如何确定 setup.py 中实际需要哪些要求?
- c++11 - 错误:使用 make all 编译 Liquibook 时没有名为 'emplace' 的成员
- javascript - JavaScript 意外的令牌语法错误
- angular - Angular 6 材质表数据源
- sql - SQL SRSS 创建一个在添加用户输入值后更改的列表