布尔属性无法快速标记为动态 [英] Bool Property Cannot be marked dynamic in swift
问题描述
我正在尝试使用KVO快速观察Bool
值,并添加这样的dynamic
修饰符:
I'm trying to observe Bool
value in swift using KVO and add dynamic
modifier like this :
dynamic var isRestricted:Bool?
和编译器说
无法将属性标记为动态,因为其类型不能为 代表在Objective-C
Property cannot be marked dynamic because its type canot be represented in Objective-C
那我该怎么办?我应该为此更改为NSNumber吗?那么观察价值的最佳实践是什么?
then what should I do? should I change to NSNumber for this? and What is the best practice for observing value then?
我使用xcode 7 beta 2
im using xcode 7 beta 2
推荐答案
实际问题是无法表示 optional 布尔值 在Objective-C中(因此未标记为动态).使用非可选
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
应该解决问题.
通常,Objective-C
中不存在可选"的概念,
但是对NSObject
子类实例的可选引用是
在Objective-C中桥接到可为空的对象指针,所以
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?
仅当Foo
是NSObject
的子类时,才允许
.
is allowed if (and only) if Foo
is a subclass of NSObject
.
这篇关于布尔属性无法快速标记为动态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!