当我将我的应用程序转换为新技术ARC(自动引用计数)时,从CoreData获取时出现错误。以下是我的方法,并呼吁它:
NSArray *userinfo =[self checkData:self.username];
//This return fault data in array
-(NSArray*) checkData:(NSString*)loginUsername{
id appDelegate = (id)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *managedObjectContext = [[NSManagedObjectContext alloc] init];
[managedObjectContext setPersistentStoreCoordinator:[[appDelegate managedObjectContext] persistentStoreCoordinator]];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *userEntity = [NSEntityDescription entityForName:@"User" inManagedObjectContext:managedObjectContext];
[request setEntity:userEntity];
[request setPredicate:[NSPredicate predicateWithFormat:@"userName == %@",loginUsername ]];
NSError *error;
NSArray *userInfo =[managedObjectContext executeFetchRequest:request error:&error];
//NSLog(@"Number of account%@",[userInfo valueForKey:@"userName"]);
if ([userInfo valueForKey:@"userName"] != nil && userInfo.count != 0)
return userInfo;
else return nil;
}
以下是崩溃日志:
<User: 0x12b73db0> (entity: User; id: 0x12b6dc20 <x-coredata://7388F0B7-E583-4BF0-BE64-08AD014EC583/User/p2> ; data: <fault>)
2012-12-23 10:09:18.802 test[78839:c07] -[NSNull isEqualToString:]: unrecognized selector sent to instance 0x28ac678
2012-12-23 10:09:18.803 test[78839:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull isEqualToString:]: unrecognized selector sent to instance 0x28ac678'
*** First throw call stack:
(0x277b012 0x2018e7e 0x28064bd 0x276abbc 0x276a94e 0xb4028 0x3e3be 0x1812e59 0x1810f22 0x181216a 0x1810edd 0x1811055 0x175e338 0x5d3a81 0x5d2d33 0x610e3a 0x271d8fd 0x6114bc 0x611435 0x4fb3a0 0x26fef3f 0x26fea39 0x2721734 0x2720f44 0x2720e1b 0x31027e3 0x3102668 0xc5465c 0x2806 0x2735 0x1)
libc++abi.dylib: terminate called throwing an exception
答案 0 :(得分:1)
尝试使用AppDelegate的managedObjectContext:
所以替换
id appDelegate = (id)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *managedObjectContext = [[NSManagedObjectContext alloc] init];
[managedObjectContext setPersistentStoreCoordinator:[[appDelegate managedObjectContext] persistentStoreCoordinator]];
到
NSManagedObjectContext *managedObjectContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
答案 1 :(得分:0)
崩溃日志似乎说loginUsername
是零。在任何情况下,您最好在尝试使用之前检查它是否确实存在。