【发布时间】:2021-08-19 19:28:59
【问题描述】:
我有一个用 Objective-C 编写的方法,它返回一个 BOOL,例如:
(BOOL)methodName:(NSDictionary<NSString *, NSString *> *)params callback:(void(^)(NSString *_Nullable, ErrorInformation *_Nullable))callback error:(NSError *_Nullable *_Nullable)errorPtr;
在 Swift 中的使用
我得到了错误,Cannot convert value of type '()' to expected condition type 'Bool'。我认为ret 的类型是(),而不是BOOL。查看实现,这个值在 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'
}
看到这个方法有3种指示失败的方式不太好,但我没有设计它????坦率地说,我的 Objective-C 并不好:
-
errorPtr,在 Swift 中会自动变成do/try/catch -
ErrorInformation传入回调 -
BOOL返回值,我正在努力解决。
【问题讨论】:
标签: swift objective-c