1

When overriding in a Swift subclass of an Objective-C class, I get a message saying:

Property type 'BOOL' (aka 'bool') is incompatible with type 'Boolean' (aka 'unsigned char') inherited from 'ChildClass'

I tried to use Other Boolean types but it won't work.

Any idea how to properly override an Objc BOOL in Swift

Swift code (subclass):

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

Objc Parent Declaration:

@property(atomic) Boolean isLoading;

Swift bridging header where the warning appear:

SWIFT_CLASS("_TtC6Module30ChildClass")
@interface ChildClass : ParentClass
@property (nonatomic) BOOL myVar; //<----- Here 
@end
Antzi
  • 12,831
  • 7
  • 48
  • 74

1 Answers1

1

in ObjC BOOL and bool are not the same (BOOL is a signed char, whereas bool - aka C bool- is an unsigned char). Boolean is typedef to unsigned char as well, so is a C bool. Can you change your objC property to BOOL? if not, then use the swift type 'CBool'.

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

Florian Burel
  • 3,408
  • 1
  • 19
  • 20
  • If I change the `Swift` type then it complain about incompatibilities with `bool`. Indeed, Using `BOOL` in the Objc parent did the trick. I'm so used to using only `BOOL` in objc I did'nt even noticed the parent had a different type – Antzi Mar 30 '18 at 08:19