Home > Software design >  In Swift, an Objective-C BOOL inferred as `()` instead of Bool
In Swift, an Objective-C BOOL inferred as `()` instead of Bool

Time:09-16

I've got a method written in Objective-C which returns a BOOL, for example:

 (BOOL)methodName:(NSDictionary<NSString *, NSString *> *)params callback:(void(^)(NSString *_Nullable, ErrorInformation *_Nullable))callback error:(NSError *_Nullable *_Nullable)errorPtr;

Usage in Swift

I get the error, Cannot convert value of type '()' to expected condition type 'Bool'. I thinks that ret is of type (), instead of BOOL. Looking at the implementation, this value is mutated inside dispatch_sync.

let ret = try! methodName()
// I've tried a bunch of different syntaxes below:
if (ret) { <--- Xcode warning: Cannot convert value of type '()' to expected condition type 'Bool'

}

It is not nice to see this method has 3 ways of indicating failure, but I didn't design it

  • Related