Why doesn't IBInspectable set vaue for an optional variable from the storyboard?

I am doing a demo to learn how @IBInspectable works.

First, I made two variables in a subclass of UIViewController:

  @IBInspectable var intTest:Int = 10
  @IBInspectable var flag:Bool = false

So that he appears in the storyboard. After that, I change the value of these variables in the storyboard:

enter image description here

Then I try to print these variables in the viewDidLoad () method as follows:

  override func viewDidLoad() {
       super.viewDidLoad()
       // Do any additional setup after loading the view, typically from a nib.
       print("intTest value: \(String(describing: intTest))")
       print("flag value: \(String(describing: flag)))")
  }

Conclusion:

IntTest value: 90

Flag value: true

It works great, except when I change the declaration of variables, for example,

  @IBInspectable var intTest:Int? = 10
  @IBInspectable var flag:Bool? = false

After that, I changed the value as indicated above in the storyboard, but the values ​​do not change.

Output:

intTest value: optional (10)

Flag Value: Optional (false)

So my question is: why is the value not updated when I declare the variable as optional?

, , . - ?

+4
2

IBInspectable var

@IBInspectable var placeHolderImage:UIImage? {
     didSet {
         leftViewSetting()
     }
 }

IBInspectable var

@IBInspectable var placeHolderImage:UIImage = UIImage() {
    didSet {
        leftViewSetting()
    }
}

, Interface Builder`, .

@IBInspectable var cornerRadius: CGFloat {
    get {
       return layer.cornerRadius
    }
    set {
       layer.cornerRadius = newValue
    }
}
+1

, . , :

 @IBInspectable var intTest: Int? {
        didSet {
            self.intTestTweak = intTest
        }
  }

willSet didSet .

0

Source: https://habr.com/ru/post/1676326/


All Articles