7

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
         }
      }
   }
A O
  • 5,516
  • 3
  • 33
  • 68

1 Answers1

8

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

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • 1
    *"a Swift optional is not representable in Objective-C"* — well, this is not true for optional classes / protocol types, but it is true for structs/enums, of which CGFloat is one. – jtbandes Mar 03 '16 at 22:38