【问题标题】:BOOL is incompatible with Bool in Swift 4.1BOOL 与 Swift 4.1 中的 Bool 不兼容
【发布时间】:2018-03-30 07:12:25
【问题描述】:

在 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
    }
}

Objc 父声明:

@property(atomic) Boolean isLoading;

出现警告的 Swift 桥接头:

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

【问题讨论】:

  • 作为?布尔 - 也不起作用?
  • @SergeyHleb 抱歉,我不太清楚。我添加了代码来消除歧义。问题在于桥接头的声明
  • 您实际上是否在使用/覆盖isLoading 变量?
  • 也许this可以帮助你?

标签: objective-c swift swift4.1


【解决方案1】:

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

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

【讨论】:

  • 如果我更改 Swift 类型,那么它会抱怨与 bool 不兼容。实际上,在 Objc 父级中使用 BOOL 就可以了。我已经习惯了在 objc 中只使用 BOOL 我什至没有注意到父母有不同的类型
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-07
  • 1970-01-01
  • 2017-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多