Why can I not use KVC from an Objective-C object to a Swift Property?

北城余情 提交于 2019-12-07 01:43:52

问题


My team has decided that new files should be written in swift, and I am seeing an odd problem with using KVC in an Objective-C object to set a property on a Swift object.

My Objective-C sets a property like so: [textObject setValue:0.0 forKey:@"fontSize"]

My Swift object (textObject) has a custom setter/getter for this property.

   var fontSize: CGFloat? {
      get {
         return internalTextGraphic?.fontSize 
      }
      set {
            internalTextGraphic?.fontSize = newValue
      }
   }

However, if I set a breakpoint in the set, it never gets hit.

I have Objective-C objects that also get this same call, and I just implement -setFontSize, and the execution enters properly.

Why can't I seem to get into my set method through -setValueForKey? I have 100% confirmed the textObject is exists and is the correct type.

EDIT:
Martin R is correct, I had to make the type a non-optional. This is my working code:

   var fontSize: CGFloat {
      get {
         var retFontSize: CGFloat = 0.0
         if let fontSize = internalTextGraphic?.fontSize {
            retFontSize = fontSize
         }
         return retFontSize
      }
      set {
         if let textGraphic = internalTextGraphic {
            textGraphic.fontSize = newValue
         }
      }
   }

回答1:


The reason is that a Swift optional struct or enum (in your case CGFloat?) is not representable in Objective-C (and you won't see that property in the generated "Project-Swift.h" header file). That becomes more obvious if you mark the property explicitly with @objc, then you'll get the error message

error: property cannot be marked @objc because its type cannot be represented in Objective-C

If you change the property type to the non-optional CGFloat then KVC works as expected. It would also work with an optional class type, such as NSNumber?.



来源:https://stackoverflow.com/questions/35783888/why-can-i-not-use-kvc-from-an-objective-c-object-to-a-swift-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!