I'm trying to observe Bool value in swift using KVO and add dynamic modifier like this :
dynamic var isRestricted:Bool?
and the compiler say
Property cannot be marked dynamic because its type canot be represented in Objective-C
then what should I do? should I change to NSNumber for this? and What is the best practice for observing value then?
im using xcode 7 beta 2
The actual problem is that optional booleans cannot be represented in Objective-C (and therefore not marked dynamic). Using a non-optional
dynamic var isRestricted : Bool = false
should solve the problem.
Generally, the concept of "optionals" does not exist in Objective-C,
but optional references to instances of NSObject subclasses are
bridged to nullable object pointers in Objective-C, so
dynamic var foo: Foo?
is allowed if (and only) if Foo is a subclass of NSObject.
来源:https://stackoverflow.com/questions/31131149/bool-property-cannot-be-marked-dynamic-in-swift