gpt4 book ai didi

ios - objective-c 完成 block 问题

转载 作者:行者123 更新时间:2023-11-28 21:53:08 26 4
gpt4 key购买 nike

我试图在异步任务中获取地址簿的 JSON (NSString *),但我希望它是异步的并带有一个完成 block 。

我已经设法在没有完成 block 的情况下轻松检索,但是当我添加完成 block 时出现以下编译器错误:

不兼容的 block 指针类型将 NSString *(^)(bool, CFErrorRef)' 传递给 'ABAddressBookRequestAccessCompletionHandler' 类型的参数(又名 'void (^)(bool, CFErrorRef)')

这是我的代码

+ (NSString *) getAddressBook:(id (^)())block {
ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();

if (status == kABAuthorizationStatusDenied)
{
// if you got here, user had previously denied/revoked permission for your
// app to access the contacts, and all you can do is handle this gracefully,
// perhaps telling the user that they have to go to settings to grant access
// to contacts
[[[UIAlertView alloc] initWithTitle:nil message:@"This app requires access to your contacts to function properly. Please visit to the \"Privacy\" section in the iPhone Settings app." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
return nil;
}
CFErrorRef error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);

if (error)
{
NSLog(@"ABAddressBookCreateWithOptions error: %@", CFBridgingRelease(error));
if (addressBook) CFRelease(addressBook);
return nil;
}

if (status == kABAuthorizationStatusNotDetermined)
{
// present the user the UI that requests permission to contacts ...
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) -> THE ERROR OCCURS HERE !
{...

我认为问题出在我的完成 block 中,但我找不到针对我的特定问题的一些好的示例/教程,如果有人能帮助我,我会非常高兴。

最佳答案

你的问题是你像这样声明 getAddressBook(如果我们剥离 block )

-(id)block

并且您正在尝试将其作为类型函数传递:

-(void)block:(BOOL)aBool error:(CFErrorRef*)error

block 很难与语法一起使用(这在 swift 中得到了改进)但是我总是在不确定时引用这个网站:

block syntax reference

更新:

您实际上必须等待您的异步方法完成。您也可以引用official documentation

+ (void) getAddressBook:((^)(NSString* result))block {
ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();

if (status == kABAuthorizationStatusDenied)
{
// if you got here, user had previously denied/revoked permission for your
// app to access the contacts, and all you can do is handle this gracefully,
// perhaps telling the user that they have to go to settings to grant access
// to contacts
[[[UIAlertView alloc] initWithTitle:nil message:@"This app requires access to your contacts to function properly. Please visit to the \"Privacy\" section in the iPhone Settings app." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
block(nil);
}
CFErrorRef error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);

if (error)
{
NSLog(@"ABAddressBookCreateWithOptions error: %@", CFBridgingRelease(error));
if (addressBook) CFRelease(addressBook);
block(nil);
}

if (status == kABAuthorizationStatusNotDetermined)
{


// present the user the UI that requests permission to contacts ...
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) -> THE ERROR OCCURS HERE !
{
NSString* result = whatever

block(result);
});
}
}

然后像这样使用它:

[YourClass getAddressBook:^(NSString* result)
{
//now you really have result
}];

关于ios - objective-c 完成 block 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27542187/

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