【问题标题】:Printing address book to console with NSLog使用 NSLog 将地址簿打印到控制台
【发布时间】:2014-01-13 03:56:14
【问题描述】:

我在我的viewWillAppear: 中使用它来将我所有的联系人姓名和号码打印到我的控制台。

- (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'***

有什么问题?

EDIT 发现抛出异常的断点问题: 完整的.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;
}

问题出在//configure the cells 下方的cell.textLabel.text = self.contactsObjects[indexPath.row]; 上。在cellForRowAtIndexPath: 方法中。

【问题讨论】:

  • 哪条线路崩溃了?
  • 对不起,我不确定那个值是多少。如果这是 java 我可以,但我不知道 Xcode 上的位置(现在列出了完整的调试器报告)
  • 哇,这应该没那么难。我花了一整天的时间试图弄清楚如何将我的联系人打印到我的控制台,以确保我真的抓住了他们。
  • 您想在表格中显示什么电话号码或用户名?
  • 用户名。我只需要在表格中显示用户名。或者我猜这将是他们的真名?因为这只是从我在 my iPhone 上的联系人列表中提取的。

标签: ios objective-c xcode abaddressbook nslog


【解决方案1】:

这意味着您试图向地址 0x175c4b10 处的对象询问其长度,但它不是响应方法 length 的对象。该错误可能不在您发布的代码中,因为您没有在那里对length 方法进行任何调用。可能是您在其他地方调用length(搜索您的代码在哪里),或者可能是您调用的方法最终调用length,但您传递给它的参数无效。

【讨论】:

  • 有没有办法告诉我这个实例来自哪个类?
  • 其实这是个愚蠢的问题,因为我知道它在哪个页面崩溃
  • 您可以在 Objective-C 异常上添加断点。为此,请打开断点窗格,单击“+”按钮,然后选择“添加异常断点...”。默认情况下,它应该停止 C++ 和 Objective-C 异常(我认为)。这应该会导致您的程序在遇到问题行后立即停止在调试器中。
  • 啊,那么断点会出现在源头吗?抱歉,我什至从未使用过断点..
  • 你是对的。我在这里找到了如何使用断点:stackoverflow.com/questions/7354169/…
【解决方案2】:
#define CHECK_NULL_STRING(str) ([str isKindOfClass:[NSNull class]] || !str)?@"":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)]

【讨论】:

  • 天才!它有效,但……它在每个单元格中打印相同的联系人……我现在正试图找出原因……
  • 我认为是 objectAtIndex:0。我也想包括姓氏
  • 是的.. 如果我将其更改为 objectAtIndex:1] 它会打印出不同的名称,并为 tableView 中的每个单元格打印该名称。
  • indexpath.row 使用这个
  • 不错。有效。伙计,你的好大声笑。最后一个问题..你会碰巧知道如何包括姓氏吗?比如,就在名字之后?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-03
相关资源
最近更新 更多