1

在 Objective-C 类的 Swift 子类中重写时,我收到一条消息:

属性类型'BOOL'(又名'bool')与继承自'ChildClass'的类型'Boolean'(又名'unsigned char')不兼容

我尝试使用其他布尔类型,但它不起作用。

知道如何在 Swift 中正确覆盖 Objc BOOL

Swift 代码(子类):

override var myVar: Bool {
    get {
        return something ? true : myVar
    }
    set {
        myVar = newValue
    }
}

对象父声明:

@property(atomic) Boolean isLoading;

出现警告的 Swift 桥接头:

SWIFT_CLASS("_TtC6Module30ChildClass")
@interface ChildClass : ParentClass
@property (nonatomic) BOOL myVar; //<----- Here 
@end
4

1 回答 1

1

在 ObjC 中,BOOL 和 bool 是不一样的(BOOL 是有符号字符,而 bool - 也就是 C bool- 是无符号字符)。Boolean 也是 unsigned char 的 typedef,C bool 也是如此。您可以将 objC 属性更改为 BOOL 吗?如果没有,则使用快速类型“CBool​​”。

https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithCAPIs.html

于 2018-03-30T07:44:49.593 回答