我正在尝试使用NSLog()
将所有手机的联系人打印到控制台。目前此代码只是打印(null)
。
·H
@property (nonatomic, strong) NSMutableArray *contactsObjects;
的.m
@synthesize contactsObjects;
//lazy instantiation.
- (NSMutableArray *)contactsObjects
{
if(!contactsObjects)
{
contactsObjects = [[NSMutableArray alloc]init];
}
return contactsObjects;
}
- (void)viewWillAppear:(BOOL)animated
{
CFErrorRef error = nil;
// Request authorization to Address Book
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, &error);
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
if (granted) {
// First time access has been granted, add all the user's contacts to array.
CFMutableArrayRef contactsObjects = ABAddressBookCopyArrayOfAllPeople(addressBookRef);
} else {
// User denied access.
// Display an alert telling user that they must allow access to proceed to the "invites" page.
}
});
}
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
// The user has previously given access, add all the user's contacts to array.
CFMutableArrayRef contactsObjects = ABAddressBookCopyArrayOfAllPeople(addressBookRef);
}
else {
// The user has previously denied access
// Send an alert telling user that they must allow access to proceed to the "invites" page.
}
NSLog(@"%@", contactsObjects);
}
我在这里收到两个警告:
我不知道为了正确地将我的联系人的姓名和号码打印到控制台,我应该做些什么。
如何打印联系人姓名和号码?
答案 0 :(得分:2)
你说它打印出null并且你得到一个错误。但这可以解释你的错误。
contactObjects
在if块和else if块中定义。因此,当您超出条件时,它就不再被定义了。
试试这个
- (void)viewWillAppear:(BOOL)animated
{
CFErrorRef error = nil;
// Request authorization to Address Book
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, &error);
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
if (granted) {
// First time access has been granted, add all the user's contacts to array.
contactsObjects = ABAddressBookCopyArrayOfAllPeople(addressBookRef);
} else {
// User denied access.
// Display an alert telling user that they must allow access to proceed to the "invites" page.
}
});
}
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
// The user has previously given access, add all the user's contacts to array.
contactsObjects = ABAddressBookCopyArrayOfAllPeople(addressBookRef);
}
else {
// The user has previously denied access
// Send an alert telling user that they must allow access to proceed to the "invites" page.
}
NSLog(@"%@", contactsObjects);
}
答案 1 :(得分:2)
您的代码存在范围问题。 contactsObjects
中的viewWillAppear:
个变量与您调用的contactsObjects
的ivar无关。您正在声明使用相同名称的新变量。另一方面,最后的NSLog()
是伊瓦尔。但设置其他变量并没有在ivar中添加任何内容,因此您会看到(null)
,NSLog()
代表“无对象”。
通过不使用新的变量声明来修复此问题,但使用ivar。
if (granted) {
contactsObjects = ABAddressBookCopyArrayOfAllPeople(addressBookRef);
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
contactsObjects = ABAddressBookCopyArrayOfAllPeople(addressBookRef);
您还需要投射这些:
contactsObjects = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBookRef);
(此外,该函数不会返回一个可变数组,所以你可能会遇到麻烦。)
第二个问题是ABAddressBookRequestAccessWithCompletion()
没有停止并等待其完成Block运行。在请求访问权限时,您的方法的其余部分会继续执行,因此在此情况下实际设置NSLog()
之前,您会到达contactsObjects
。