gpt4 book ai didi

objective-c - 无法为使用 typedef 的 block 编写正确的语法

转载 作者:行者123 更新时间:2023-11-30 10:22:06 25 4
gpt4 key购买 nike

我有 Objective-C 代码,我尝试将其转换为 Swift 但失败了。

typedef void (^ CDVAddressBookWorkerBlock)(
ABAddressBookRef addressBook,
CDVAddressBookAccessError* error
);

@interface CDVAddressBookHelper : NSObject
{}

- (void)createAddressBook:(CDVAddressBookWorkerBlock)workerBlock;
@end

这是 Objective-C 实现:

 CDVAddressBookHelper* abHelper = [[CDVAddressBookHelper alloc] init];
[abHelper createAddressBook:
^(ABAddressBookRef addrBook, CDVAddressBookAccessError* errorCode)
{
/* ...*/
}
];

如何用 Swift 编写它?

来自文档:

{(parameters) -> (return type) in expression statements}

这是 xCode 提供的模板:

enter image description here

这是我尝试过的:

var abHelper:CDVAddressBookHelper = CDVAddressBookHelper()

abHelper.createAddressBook(
{(addrBook:ABAddressBookRef, errCode:CDVAddressBookAccessError) in

if addrBook == nil {

}
} )

错误:

 Type 'ABAddressBook!' does not conform to protocol 'AnyObject'`

[编辑]

问候:swift-closure-declaration-as-like-block-declaration帖子我尝试编写typealias:

typealias CDVAddressBookWorkerBlock = (addrBook:ABAddressBookRef, errCode:CDVAddressBookAccessError) -> ()

接下来怎么办?

如何让它发挥作用?

谢谢

最佳答案

查看 docs关于如何使用 Cocoa 和 Core Foundation。

这应该有效:

abHelper.createAddressBook() {
(addrBook:ABAddressBook?, errCode:CDVAddressBookAccessError!) in

if addrBook == nil {

}


}

关于objective-c - 无法为使用 typedef 的 block 编写正确的语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25571087/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com