NSArrayM insertObject:atIndex:]: 对象不能为 nil
Posted
技术标签:
【中文标题】NSArrayM insertObject:atIndex:]: 对象不能为 nil【英文标题】:NSArrayM insertObject:atIndex:]: object cannot be nil 【发布时间】:2013-06-03 06:03:27 【问题描述】:嗨,这里是下面的函数,我从函数中收集 NSString
值,例如名字和姓氏,并分配到它的数组中
- (void)getPersonOutOfAddressBook
ABAddressBookRef addressBook = ABAddressBookCreate();
if (addressBook != nil)
NSLog(@"Succesful.");
NSArray *allContacts = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
NSUInteger i = 0;
NSLog(@" Result :%d",[allContacts count]);
for (i = 0; i < [allContacts count]; i++)
ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];
NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);
NSString *lastName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);
NSMutableArray *name = [NSMutableArray array];
[name addObject:firstName];
[name addObject:lastName];
[self displaynames:name];
//NSLog(@"FirstNAme::::: LastNAME::::MobileNUMb::::EMail::: %@ %@",firstName,lastName);
ABMultiValueRef mobile=ABRecordCopyValue(contactPerson, kABPersonPhoneProperty);
for (int k=0;k<ABMultiValueGetCount(mobile); k++)
NSString *mobileNo = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(mobile, k);
// NSLog(@"mobile number: %@",mobileNo);
-(void)displaynames:(NSMutableArray*)names
for(NSMutableArray* name in names)
NSLog(@"MyResult:%@ %@",[names objectAtIndex:0],[names objectAtIndex:1]);
messageToSend = [messageToSend stringByAppendingString:[names objectAtIndex:0]];
messageToSend = [messageToSend stringByAppendingString:[names objectAtIndex:1]];
还有发送数据
- (IBAction)sendContacts:(id)sender
NSData *textData = [self.messageToSend dataUsingEncoding:NSASCIIStringEncoding];
//send data to all connected devices
[self.session sendDataToAllPeers:textData withDataMode:GKSendDataReliable error:nil];
这里 messageToSend 是一个 NSMutableString
所以在上面的代码中,我收集了所有名字和姓氏并将其放入数组中。当我在 iphone 中运行它时,它显示错误 [__NSArrayM insertObject:atIndex:]: object cannot be nil'
在NSLog(@"Myresult");
之后,它会显示名字和姓氏两次。所以任何人都可以告诉我我哪里出错了,以及我如何收集电话号码以及如何收集电话号码。我必须在上面的代码中进行哪些更改以避免该错误和它只显示几个名字而不是全部。
显示
2013-06-03 11:40:12.524 GetContacts[1173:1bb03] Succesful.
2013-06-03 11:40:12.539 getlist[1173:1bb03] Result :2
2013-06-03 11:40:12.567 getlist[1173:1bb03] MyResult:john cena
2013-06-03 11:40:12.568 getlist[1173:1bb03] Successsssss::::::
2013-06-03 11:40:12.569 getlist[1173:1bb03] MyResult:john cena
2013-06-03 11:40:12.571 getlist[1173:1bb03] Successsssss::::::
2013-06-03 11:40:12.734 getlist[1173:1bb03] MyResult:peter john
2013-06-03 11:40:12.735 getlist[1173:1bb03] Successsssss::::::
2013-06-03 11:40:12.736 getlist[1173:1bb03] MyResult:peter john
2013-06-03 11:40:12.737 getlist[1173:1bb03] Successsssss::::::
【问题讨论】:
messageToSend 分配在哪里? 在 ViewDidload 方法中 请检查您将名字和姓氏分配给 NSMutableArray 的位置是否为零?请在将对象添加到数组之前放置 NsLog 或在将对象插入数组之前使用 if(firstname)[add object];if(lastname)[addobject] 当你向数组添加对象时,请检查它是否为零?如果它是 nil,则不要添加到数组中,否则将其添加到数组中。你说清楚了吗? [name addObject:firstName]; [名称添加对象:姓氏];在添加对象之前检查名字,姓氏不为空 【参考方案1】:首先检查你放入数组中的数据是否为nil,如果它只是跳过它。
类似这样的:
if(firstName != nil)
[yourMutableArray addObject:firstname];
你明白了。
【讨论】:
是的,我明白了,我把代码放在了上面,但是我会写什么 else 语句??如果我跳过 else 它工作正常?? @user2443391 是的,你可以跳过其他部分 @RahulVyas 谢谢你,它的工作方式与我如何在上述代码中检索电话号码的方式相同......我还必须进行哪些更改才能获得电话号码 @RahulVyas 谢谢你,它的工作方式与我如何在上述代码中检索电话号码的方式相同......我还必须进行哪些更改才能获得电话号码【参考方案2】:要访问大部分内容,请使用以下代码
CFStringRef firstName = ABRecordCopyValue(record , kABPersonFirstNameProperty);
CFStringRef LastName = ABRecordCopyValue(record, kABPersonLastNameProperty);
CFStringRef companyRef = ABRecordCopyValue(record, kABPersonOrganizationProperty);
CFDateRef birthDate = ABRecordCopyValue(record, kABPersonBirthdayProperty);
CFStringRef prefixName = ABRecordCopyValue(record , kABPersonPrefixProperty);
CFStringRef phoneticFirstName = ABRecordCopyValue(record , kABPersonFirstNamePhoneticProperty);
CFStringRef phoneticLastName = ABRecordCopyValue(record , kABPersonLastNamePhoneticProperty);
CFStringRef middleName = ABRecordCopyValue(record , kABPersonMiddleNameProperty);
CFStringRef suffixName = ABRecordCopyValue(record , kABPersonSuffixProperty);
CFStringRef nickName = ABRecordCopyValue(record , kABPersonNicknameProperty);
CFStringRef jobTitle = ABRecordCopyValue(record , kABPersonJobTitleProperty);
CFStringRef department = ABRecordCopyValue(record , kABPersonDepartmentProperty);
CFStringRef notes = ABRecordCopyValue(record , kABPersonNoteProperty);
NSString *LastNameStr = (NSString *)LastName;
NSString *firstNameStr = (NSString *)firstName;
NSString *companyStr = (NSString *)companyRef;
NSDate *birthDateValue = (NSDate *)birthDate;
NSString *prefixNameStr = (NSString *)prefixName;
NSString *phoneticFirstNameStr = (NSString *)phoneticFirstName;
NSString *phoneticLastNameStr = (NSString *)phoneticLastName;
NSString *middleNameStr = (NSString *)middleName;
NSString *suffixNameStr = (NSString *)suffixName;
NSString *nickNameStr = (NSString *)nickName;
NSString *jobTitleStr = (NSString *)jobTitle;
NSString *departmentStr = (NSString *)department;
NSString *notesStr = (NSString *)notes;
【讨论】:
【参考方案3】:如果你错误地推送了一些视图控制器而不是另一个视图控制器,你会得到这个错误。对我来说..
我的代码类似于.. MainViewController *mainVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"mainView"];
[self.navigationController presentViewController:mainVC animated:YES completion:nil];
而不是 .... SWRevealViewController *mainVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"revealView"];
[self.navigationController presentViewController:mainVC animated:YES completion:nil];
所以请确保您正在推动正确的视图控制器。我希望这对某人有所帮助。
【讨论】:
以上是关于NSArrayM insertObject:atIndex:]: 对象不能为 nil的主要内容,如果未能解决你的问题,请参考以下文章
无法将“__NSArrayM”类型的值转换为“NSDictionary”
NSArrayM 中的异常 insertObject:atindex
集合 <__NSArrayM: 0x7fa1f2711910> 在枚举时发生了突变
在映射集合时快速获取异常“所需类型 = NSOrderedSet;给定类型 = __NSArrayM”