我在我的ViewController中使用NSManagedObject作为属性,声明如下:
@property(retain, nonatomic)NSManagedObject *person;
我将获取的内容传播到UITableView,当用户点击他正在寻找的联系人时,会发生以下情况:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
self.person = [contacts objectAtIndex:indexPath.row];
联系人包含获取的结果,其执行方式如下:
NSArray *contactsArray;
NSError *error;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSManagedObjectContext *context = [(OAuthStarterKitAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Contacts" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
testForTrue = [NSPredicate predicateWithFormat:@"SOME CONDITION"];
[fetchRequest setPredicate:testForTrue];
[fetchRequest setFetchLimit:10];
contactsArray = [[NSArray alloc]initWithArray:[context executeFetchRequest:fetchRequest error:&error]];
当用户点击联系人时,self.person有该值,但当我尝试在另一个方法中使用该值时,它为零,地址为0x000000。 这只发生在iOS 5上,在iOS 6上有人选择了联系人的价值,我可以在其他地方使用。
有什么想法吗?
感谢。
答案 0 :(得分:0)
而不是获取始终依赖于上下文生命周期的NSManagedObject
我只是存储de NSManagedObjectID
然后使用函数{{1}获取NSManagedObject
}。它有效!