0

当我将我的应用程序转换为新技术 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
4

2 回答 2

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];
于 2012-12-23T07:21:13.193 回答
0

崩溃日志似乎说那loginUsername是零。无论如何,您最好在尝试使用它之前检查它是否确实存在。

于 2012-12-23T10:38:52.127 回答