2 daffodilgirl DaffodilGirl 于 2012.12.24 09:54 提问

返回fetch请求方法的对象时出错

在应用自动升级为新的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

1个回答

Mr_me
Mr_me   2012.12.24 10:51
已采纳

换成用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];
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!