0

I'm using this in my viewWillAppear: to print all my contacts names and numbers to my console.

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

and here's the output

With this error message:

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

What's the problem?

EDIT problem found with breakpoint for Exception on throw: 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;
}

Problem is on cell.textLabel.text = self.contactsObjects[indexPath.row]; underneath the //configure the cells. in the cellForRowAtIndexPath: method.

Chisx
  • 1,976
  • 4
  • 25
  • 53
  • What line is crashing? – jscs Jan 13 '14 at 04:06
  • sorry man I'm not sure were that value is. If this were java I could but I don't know where that is on Xcode (full debugger report is now listed) – Chisx Jan 13 '14 at 04:16
  • Wow this should not be that difficult. I've spent all day just trying to figure out how to print my contacts to my console just to make sure that I've actually grabbed them. – Chisx Jan 13 '14 at 04:45
  • what you want to show in the table Phone Number or user name? – Sunny Shah Jan 13 '14 at 05:09
  • Username. I just need the username to show in the table. Or I guess it'll be their actual name? Since this is just pulled from my contacts list on *my* iPhone. – Chisx Jan 13 '14 at 05:11

2 Answers2

1

It means that you've attempted to ask the object at address 0x175c4b10 for its length, but it is not an object that responds to the method length. The error is probably not in the code you've posted, since you do not make any calls to the length method there. It could be that you're calling length somewhere else (search your code for where), or it could be that you're calling a method which eventually calls length, but you're passing it an invalid parameters.

user1118321
  • 25,567
  • 4
  • 55
  • 86
  • Is there a way that I can tell which class this instance is coming from? – Chisx Jan 13 '14 at 04:28
  • Actually that's a stupid question because I know what page it crashes on – Chisx Jan 13 '14 at 04:30
  • You can add a breakpoint on Objective-C exceptions. To do this, open the breakpoints pane, click the "+" button, and choose "Add Exception Breakpoint…". By default it should stop on C++ and Objective-C exceptions (I think). That should cause your program to stop in the debugger as soon as it hits the problem line. – user1118321 Jan 13 '14 at 04:31
  • Ahh, and so the breakpoint will then appear at the source? sorry I've never even used breakpoints.. – Chisx Jan 13 '14 at 04:32
  • you're right. I found out how to use the breakpoints here: http://stackoverflow.com/questions/7354169/breaking-on-unrecognized-selector – Chisx Jan 13 '14 at 04:57
0
#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)]
Sunny Shah
  • 12,990
  • 9
  • 50
  • 86
  • Genius! It works, buttt.. it's printing the same contact over in every cell.. and I'm trying to figure out why right now... – Chisx Jan 13 '14 at 05:28
  • I think it's the objectAtIndex:0. Also I'd like to include the last names too – Chisx Jan 13 '14 at 05:29
  • Yeah.. if I change it to `objectAtIndex:1]` it prints out a different name, and prints that name for every cell in the tableView. – Chisx Jan 13 '14 at 05:31
  • indexpath.row use this – Sunny Shah Jan 13 '14 at 05:32
  • NICE. it worked. Dude your good lol. One last question.. would you happen to know how to also include last names? like, right after first names? – Chisx Jan 13 '14 at 05:38
  • use of undeclared identifier `'allNames'`. what is that variable? An NSObject? – Chisx Jan 13 '14 at 05:43
  • HAHA I figured that last one out at the same time – Chisx Jan 13 '14 at 05:48
  • sorry check it out.. that was my array you should replace your array noyhing else – Sunny Shah Jan 13 '14 at 05:48
  • dude thank you soooo much. You just saved me like 4 hours of restless internet surfing. – Chisx Jan 13 '14 at 05:49
  • Hey one last question. Any easy way to get the "(null)" names to not show? Like I have some contacts that are missing last names, or first I'm not really sure, but it actually shows `(null)` for the people who don't have both names listed – Chisx Jan 13 '14 at 05:50
  • +1+1 Unbelievable dude. You are the best stack answerer I've ever run across. Props. – Chisx Jan 13 '14 at 05:58