Я использую это в своем 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'***
В чем проблема?
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;
}
Проблема на cell.textLabel.text = self.contactsObjects[indexPath.row];
нижней стороне //configure the cells
. в методе cellForRowAtIndexPath:
.
Какая линия рушится? –
Извините, я не уверен, что это значение. Если бы это была java, я мог бы, но я не знаю, где это находится на Xcode (теперь отображается полный отчет отладчика) – Chisx
Ничего себе, это не должно быть так сложно. Я потратил весь день, пытаясь понять, как печатать мои контакты на моей консоли, чтобы убедиться, что я их схватил. – Chisx