将块从 Objective-C 转换为 Swift
Converting a block from Objective-C to Swift
我写了下面的方法 returns 我在 Objective-C 中写的一个块。无论我多少次弄乱语法,我都无法获得编译器喜欢的此方法的 swift 版本。
- (TWCInviteAcceptanceBlock)acceptHandler
{
return ^(TWCConversation * _Nullable conversation, NSError * _Nullable error) {
if (conversation) {
NSLog("Yay")
}
else {
NSLog(@"Boo")
}
};
}
有什么想法吗?
超出我的想象:
func acceptHandler() -> TWCInviteAcceptanceBlock {
return { (conversation: TWCConversation?, error: NSError?) in
if let conversation = conversation {
print("Yay")
} else {
print("Boo")
}
}
}
我写了下面的方法 returns 我在 Objective-C 中写的一个块。无论我多少次弄乱语法,我都无法获得编译器喜欢的此方法的 swift 版本。
- (TWCInviteAcceptanceBlock)acceptHandler
{
return ^(TWCConversation * _Nullable conversation, NSError * _Nullable error) {
if (conversation) {
NSLog("Yay")
}
else {
NSLog(@"Boo")
}
};
}
有什么想法吗?
超出我的想象:
func acceptHandler() -> TWCInviteAcceptanceBlock {
return { (conversation: TWCConversation?, error: NSError?) in
if let conversation = conversation {
print("Yay")
} else {
print("Boo")
}
}
}