Search code examples
objective-cswiftkey-value-codingkvc

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


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
         }
      }
   }

Solution

  • 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?.