Answers for "swift getter setter"

0

setter must be with getter swift

// Sure. You can just return a nil and make the type optional:

var color: MyColorEnum? {
    get { return nil }
    set {
      switch newValue! {
      case .Blue:
        view.backgroundColor = UIColor.blueColor()
      case .Red:
        view.backgroundColor = UIColor.redColor()
      }
    }
  }
// Alternatively, you may use didSet to avoid the issue all together:

var color: MyColorEnum! {
  didSet {
    switch color {
      case .Blue:
        view.backgroundColor = UIColor.blueColor()
      case .Red:
        view.backgroundColor = UIColor.redColor()
      }
  }
}
Posted by: Guest on March-26-2020

Code answers related to "Swift"

Browse Popular Code Answers by Language