我正在将一些ObjC代码转换为Swift 3,并且在一个使用过的ObjC框架中的一个类需要一个符合typedef的完成块:
目标-C:
workflow = [[Workflow alloc] initWithSettings:settings completion:^(AuthenticationAccount *account, NSError *error)
{
print("Do something ...")
}];
// The header for initWithSettings looks like this:
- (instancetype)initWithSettings:(Settings *)settings completion:(nullable authentication_account_completion_block_t)completion AUTH_DESIGNATED_INITIALIZER;
// authentication_account_completion_block_t typedef:
typedef void (^authentication_account_completion_block_t)(AuthenticationAccount * __nullable account, NSError * __nullable error);
#define AUTH_DESIGNATED_INITIALIZER NS_DESIGNATED_INITIALIZER AUTH_NO_DISCARD
如何将此正确转换为Swift,以便完成块仍符合authentication_account_completion_block_t
typedef?
如果我只是可选地将闭包强制转换为authentication_account_completion_block_t
,则块永远不会被执行:
夫特:
let workflow = Workflow(settings: settings!, completion:
{
(account:AuthenticationAccount, error:Error?) in
// Never executed!
print("Do something ...")
} as? authentication_account_completion_block_t)
workflow.start()