2014-01-13 38 views
0

我試圖使用NSLog()將所有手機的聯繫人打印到控制檯。目前這個代碼只是打印(null)從ABAddressBookRequestAccessWithCompletion()中獲取聯繫人數組給出無

.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); 
} 

我來到這裏兩個警告:

enter image description here

我不知道我我應該這樣做才能正確地將我的聯繫人的姓名和號碼打印到控制檯。

如何打印我的聯繫人姓名和電話號碼?

+0

「viewWillAppear:」中有沒有關於陰影伊娃的警告?或者關於一個未聲明的變量指向setter的錯誤? –

+0

我其實沒有。這兩個錯誤是「黃色!」錯誤,他們只是爲了我上面發佈的錯誤。 – Chisx

+0

你在什麼版本的Xcode?你明確合成'contactsObjects'嗎? –

回答

2

你必須與你的代碼的範圍問題。 viewWillAppear:中的contactsObjects變量與您稱爲contactsObjects的ivar無關。您正在聲明使用相同名稱的新變量。另一方面,最後的NSLog()是伊娃。但是設置其他變量並沒有將任何東西放入伊娃,因此您會看到(null),這是NSLog()代表「無對象」的方式。

通過不做新變量聲明,但使用伊娃來解決這個問題。

if (granted) { 
    contactsObjects = ABAddressBookCopyArrayOfAllPeople(addressBookRef); 

else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) { 
    contactsObjects = ABAddressBookCopyArrayOfAllPeople(addressBookRef); 

您還需要轉換這些:(。此外,該函數不返回可變數組,所以你可能有麻煩下來,道路)

contactsObjects = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBookRef); 

第二個問題是ABAddressBookRequestAccessWithCompletion()不停止並等待其完成Block運行。在請求訪問的同時,其餘的方法繼續進行,因此您在達到NSLog()之前實際設置contactsObjects

+0

好吧,使用'(__bridge_transfer NSArray *)'給出'不兼容的'指針類型'的警告,所以我需要重新聲明變量** contactsObjects **爲'NSArray'而不是'NSMutableArray'?你提出在'ABAddressBookRequestAccessWithCompletion()'方法之後需要一個斷點?另外,(__bridge NSMutableArray *)沒有引起任何警告,但它導致控制檯輸出地址而不是名稱或#的 – Chisx

+0

是的,更改屬性的類型。投到「NSMutableArray」是一個謊言。它不會改變數組的可變性。你不需要一個斷點。斷點用於調試。你只需要考慮到以下事實:當你請求訪問時,'contactObjects'不會被設置,直到block運行完成後,'viewWillAppear:'結束之前可能也可能不會。 –

+0

好的。我會採取的。 – Chisx

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); 
} 
+0

'contactsObjects'幾乎肯定不應該在這裏進行本地聲明;我很確定OP只是想把它放進伊娃。除此之外就是這樣。 –

+0

對不起,我必須對自己的錯誤視而不見,但我甚至都沒有看到你的代碼和我的代碼發生了變化。你介意指出你在哪裏做了改變? – Chisx

+0

等待我看到它 – Chisx