swift-keypath

More about the weird behavior of ReferenceWritableKeyPath with an Optional property

随声附和 提交于 2020-01-23 02:52:27
问题 (I say "more" in my title because this question is an appendix to my How does Swift ReferenceWritableKeyPath work with an Optional property?.) In my code, self.iv is an outlet property: @IBOutlet weak var iv: UIImageView! Now, will this compile? let im = UIImage() let kp = \UIImageView.image self.iv[keyPath:kp] = im // error No! We are told that something here must be unwrapped, although it is not entirely obvious what it is: Value of optional type 'UIImage?' must be unwrapped to a value of

How does Swift ReferenceWritableKeyPath work with an Optional property?

旧时模样 提交于 2020-01-12 07:39:09
问题 Ground of Being : It will help, before reading, to know that you cannot assign a UIImage to an image view outlet's image property through the keypath \UIImageView.image . Here's the property: @IBOutlet weak var iv: UIImageView! Now, will this compile? let im = UIImage() let kp = \UIImageView.image self.iv[keyPath:kp] = im // error No! Value of optional type 'UIImage?' must be unwrapped to a value of type 'UIImage' Okay, now we're ready for the actual use case. What I'm actually trying to