Bool Property Cannot be marked dynamic in swift

偶尔善良 提交于 2019-12-06 21:03:40

问题


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


回答1:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!