嘿伙计。我正在编写一个 ios 应用程序,其中包含多个子视图,可以通过按一下按钮单独创建。每个子视图都有一个“组”实例,即通过核心数据保存的实体。'group' 与 'contact' 是一对多的关系。当联系人被拖到子视图上时,它会保存在给定“组”的核心数据中。这可以正常工作 3 次。每 4 次将联系人拖到另一个子视图时,应用程序就会崩溃。
这是代码:
- (void)fetchContacts {
if(personRecordIDsArray==nil) {
personRecordIDsArray = [[NSMutableArray alloc] init];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Contact" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
[entity release];
NSError *error = nil;
NSMutableArray *mutableFetchResults = [[[managedObjectContext executeFetchRequest:request error:&error] mutableCopy] autorelease];
if (mutableFetchResults == nil) {
// Handle the error.
}
[error release];
[request release];
for (Contact *contact in mutableFetchResults) {
if(contact.belongsToGroup == group) {
[personRecordIDsArray addObject:contact.recordId];
}
}
}
NSLog(@"%d", [personRecordIDsArray count]);}
- (void)addContactsToGroup:(NSArray*)arrayOfPeople {
[self fetchContacts];
for(int i = 0; i<[arrayOfPeople count]; i++) {
ABRecordRef person = [arrayOfPeople objectAtIndex:i];
NSNumber *personRecordId = [NSNumber numberWithInteger:ABRecordGetRecordID(person)];
if(![self groupContainsContactWithRecordID:personRecordId]) {
NSString *compositeName = (NSString *)ABRecordCopyCompositeName(person);
//Here is when the error occurs:
Contact *contact = (Contact*)[NSEntityDescription insertNewObjectForEntityForName:@"Contact" inManagedObjectContext:managedObjectContext];
contact.compositeName = compositeName;
contact.recordId = personRecordId;
contact.belongsToGroup = group;
NSError *error = nil;
if (![managedObjectContext save:&error]) {
NSLog(@"Error in addContactsToGroup!");}
[error release];
[personRecordIDsArray addObject:personRecordId];
}
}}
尝试将联系人添加到组时,错误显示:
2011-04-10 16:16:36.152 TestApp [796:207] *由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“实体名称不得为零。”
我真的玩了一段时间,在互联网上也找不到类似的东西。有趣的是,该错误总是在第四次将联系人添加到不同的子视图时发生。有人知道我该如何解决这个问题吗?我真的没有想法......
哦,调试器说:
#0 0x956e2156 in __kill
#1 0x956e2148 in kill$UNIX2003
#2 0x95774899 in raise
#3 0x9578a9b8 in abort
#4 0x91de4fda in __gnu_cxx::__verbose_terminate_handler
#5 0x0141c4e7 in _objc_terminate
#6 0x91de317a in __cxxabiv1::__terminate
#7 0x91de31ba in std::terminate
#8 0x91de32b8 in __cxa_throw
#9 0x0141c635 in objc_exception_throw
#10 0x00ce1486 in +[NSManagedObject(_PFDynamicAccessorsAndPropertySupport) classForEntity:]
#11 0x00ce11f6 in _PFFastEntityClass
#12 0x00d06e73 in +[NSEntityDescription insertNewObjectForEntityForName:inManagedObjectContext:]
#13 0x00009a02 in -[GroupViewController addContactsToGroup:] at GroupViewController.m:152
#14 0x00002f31 in -[GrouperViewController dragingWillEnd:forContacts:atPosition:] at GrouperViewController.m:98
#15 0x0000cbb8 in -[ContactsTableViewController longPressOnView:] at ContactsTableViewController.m:65