Primitive types in Objective-C
Look at this Swift class
@objc class Foo: NSObject {
var myCGFloatOptional: CGFloat? = 1
var myCGFloat: CGFloat = 1
}
If I use it in Objective-C only the myCGFloat
property is available.
Foo * foo = [Foo new];
foo.myCGFloat;
foo.myCGFloatOptional; // Compile Error
This because in Objective-C CGFloat
is a primitive type (like float
). And Objective-C does not allow nil
values for a primitive type.
Just like you cannot write this in Objective-C
float number = 1;
number = nil; // compile error
References to instances of classes
In your Objective-C code you are able to see a property of type UIColor!
declared in Swift because UIColor
is a class. So you are declaring a pointer that could reference an instance of UIColor
or nothing.
And in Objective-C the same concept does exist.
Note
You could note that int
is a primitive type as well in Objective-C but, "unexpectedly", the compiler does allow you to write
int number = 1;
number = nil;
However in this case nil
is simply interpreted as the integer 0 (still a value, not really nil
).