ios - 计算属性设置为可选 获取为非可选
问题描述
我想创建计算属性:
var status: Int? {
get { return _status }
set {
if let newValue = newValue {
updateStatus(newValue: newValue)
}
}
}
其中 _status 是:Int。但我想得到非可选的。
user.status
没有!
如何做到最好?
解决方案
您可以提供这样的默认值
private var _status: Int
var status: Int! {
get {
return _status
}
set {
if let newValue = newValue {
_status = newValue
} else {
_status = 0
}
}
}
推荐阅读
- php - Laravel 和 LocalStack 的最大执行时间
- sql - 2 sql 使用 UNION 查询 2 个不同的结果
- jmeter - Jmeter结果配置使用grafana
- typescript - 如何在 Typescript 中映射对象并从数组中获取不同的值
- php - 我需要将百分比添加到 woocommerce 运输价值
- javascript - 如何将firebase数据库中的数据放入输入文本字段?
- java - URLConnection - 发送参数
- java - 使用正则表达式排除/删除两个特殊字符之间的字符串
- rest - 在 MTLS 保护所有其他 HTTP 请求的同时,我可以有不安全的 GET HTTP 请求吗?
- java - 如何制作顶栏?