首页 > 解决方案 > 计算属性设置为可选 获取为非可选

问题描述

我想创建计算属性:

  var status: Int? {
    get { return _status }
    set {
      if let newValue = newValue {
        updateStatus(newValue: newValue)
      }
    }
  }

其中 _status 是:Int。但我想得到非可选的。

user.status没有!

如何做到最好?

标签: iosswift

解决方案


您可以提供这样的默认值

    private var _status: Int

    var status: Int! {
    get {
        return _status
    }
    set {
        if let newValue = newValue {
            _status = newValue
        } else {
            _status = 0
        }
    }
}

推荐阅读