Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bool Property Cannot be marked dynamic in swift

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

like image 491
Dennis Farandy Avatar asked Jun 30 '15 06:06

Dennis Farandy


1 Answers

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.

like image 83
Martin R Avatar answered Nov 20 '22 00:11

Martin R