2014-01-13 36 views
0

我在我的viewWillAppear:中使用此命令將我的所有聯繫人姓名和號碼打印到控制檯。使用NSLog將控制檯打印到地址簿

- (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 = (__bridge NSMutableArray *)(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 = (__bridge NSMutableArray *)(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); 
} 

和這裏的輸出

與此錯誤消息:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType length]: unrecognized selector sent to instance 0x16ec2190'*** 

什麼問題?

編輯與斷點在拋出異常發現問題: FULL .M:

@synthesize inviteTableSearchBar; 

@synthesize contactsObjects; 
@synthesize facebookObjects; 
@synthesize twitterObjects; 
@synthesize searchResults; 

//lazy instantiations <below>------- 
- (NSArray *)contactsObjects 
{ 
    if(!contactsObjects) 
    { 
     contactsObjects = [[NSArray alloc]init]; 
    } 

    return contactsObjects; 
} 

- (NSMutableArray *)searchResults 
{ 
    if(!searchResults) 
    { 
     searchResults = [[NSMutableArray alloc]init]; 
    } 

    return searchResults; 
} 
//lazy instantiations <above>------- 

- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (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 = (__bridge_transfer NSArray *)(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 = (__bridge_transfer NSMutableArray *)(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); 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    //CUSTOM APPEARANCE <below> 

    //set in order to actually change the search bar's UIColor. 
    [self.inviteTableSearchBar setBackgroundImage:[UIImage new]]; 
    [self.inviteTableSearchBar setTranslucent:YES]; 

    self.inviteTableSearchBar.backgroundColor = [UIColor colorWithHexString:@"#333333"]; 
    [[UILabel appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor colorWithHexString:@"#669900"]]; 

    //Uncomment the following line to preserve selection between presentations; YES to clear tableView when the tableViewController recieves `viewWillAppear:` 
    self.clearsSelectionOnViewWillAppear = YES; 

    //Fill array 
    //[self.contactsObjects addObject:@"Examples<below>"]; 
    //[self.contactsObjects addObject:@"John"]; 
    //[self.contactsObjects addObject:@"Sanford"]; 
    //[self.contactsObjects addObject:@"Sally"]; 
    //[self.contactsObjects addObject:@"Susan B. Anthony"]; 

    // Hide the search bar until user scrolls up 
    CGRect newBounds = self.tableView.bounds; 
    newBounds.origin.y = newBounds.origin.y + inviteTableSearchBar.bounds.size.height; 
    self.tableView.bounds = newBounds; 

} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return self.contactsObjects.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"inviteCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    //configure the cells 
    cell.textLabel.text = self.contactsObjects[indexPath.row]; 

    return cell; 
} 

問題是cell.textLabel.text = self.contactsObjects[indexPath.row];//configure the cells下方。在cellForRowAtIndexPath:方法中。

+0

什麼行崩潰? –

+0

對不起,我不確定這個價值是什麼。如果這是java我可以但我不知道Xcode的位置(現在列出完整的調試器報告) – Chisx

+0

哇,這應該不是那麼困難。我花了整整一天的時間試圖弄清楚如何將我的聯繫人打印到控制檯上,以確保我實際上已經抓住了他們。 – Chisx

回答

0
#define CHECK_NULL_STRING(str) ([str isKindOfClass:[NSNull class]] || !str)[email protected]"":str 

id p=[contactsObjects objectAtIndex:indexPath.row]; 
    NSString *fName=(__bridge NSString *)(ABRecordCopyValue((__bridge ABRecordRef)(p), kABPersonSortByFirstName)); 
     NSString *lName=(__bridge NSString *)(ABRecordCopyValue((__bridge ABRecordRef)(p), kABPersonSortByLastName)); 
cell.textLabel.text=[NSString stringWithFormat:@"%@ %@",CHECK_NULL_STRING(fName),CHECK_NULL_STRING(lName)] 
+0

天才!它的工作原理,buttt ..它在每個單元格上打印相同的聯繫人...我試圖找出爲什麼現在... – Chisx

+0

我認爲這是objectAtIndex:0。我也想包括姓氏 – Chisx

+0

是的..如果我改變它'objectAtIndex:1]'它打印出一個不同的名稱,並打印該名稱爲每個單元格在tableView。 – Chisx

1

這意味着您試圖詢問地址0x175c4b10處的對象的長度,但它不是響應方法length的對象。該錯誤可能不在您發佈的代碼中,因爲您沒有在那裏調用length方法。可能是因爲你在其他地方打電話給length(在你的代碼中搜索你的位置),或者你可能調用了一個最終調用length的方法,但是你傳遞了一個無效的參數。

+0

有沒有一種方法可以判斷這個實例來自哪個類? – Chisx

+0

其實這是一個愚蠢的問題,因爲我知道它崩潰的頁面 – Chisx

+0

您可以在Objective-C異常中添加斷點。爲此,打開斷點窗格,單擊「+」按鈕,然後選擇「添加異常斷點...」。默認情況下,它應該停止在C++和Objective-C異常(我認爲)。這應該會導致您的程序一旦遇到問題行就停止在調試器中。 – user1118321