2

错误 ::: 2012-11-07 16:07:31.360 随机日期 [1100:c07] -[NSManagedObject 名称]:无法识别的选择器发送到实例 0x6b77240 2012-11-07 16:07:31.363 随机日期 [1100:c07] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSManagedObject name]: unrecognized selector sent to instance 0x6b77240' * First throw call stack: (0x1793022 0x1193cd6 0x1794cbd 0x16f9ed0 0x16f9cb2 0xed0a57 0xed05e4 0xed00b0 0xecf45d 0xecd2fd 0x316e 0x3481 0xdea1e 0xddfec 0x104f1d 0xef1cb 0x105df1 0x105e0d 0x105ea9 0x446f5 0x4473c 0x234f 0x15386 0x16274 0x25183 0x25c38 0x19634 0x167def5 0x1767195 0x16cbff2 0x16ca8da 0x16c9d84 0x16c9c9b 0x15c65 0x17626 0x1ffd 0x1f65 0x1) terminate called throwing an exception

主要代码如下。

{



RootViewController *rvc = [[RootViewController alloc] init];

UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:rvc];
[[self window] setRootViewController:nc];
[rvc setManagedObjectContext:[self managedObjectContext]];


self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}

在 ROOTVIEW 控制器中

enter code here
-(void)addNewRandomDate
{

NSManagedObject *newRandomDate = [NSEntityDescription insertNewObjectForEntityForName:@"RandomDate"
                                                               inManagedObjectContext:managedObjectContext];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
[dateFormatter setDateFormat:@"EEEE"];

NSDate *date = [NSDate dateWithTimeIntervalSince1970:arc4random()];
NSString *dayName = [dateFormatter stringFromDate:date];

[newRandomDate setValue:date forKey:@"date"];
[newRandomDate setValue:dayName forKey:@"dayName"];

NSError *error = nil;
BOOL success = [managedObjectContext save:&error];
if (!success) {

    NSLog(@"Error = %@", error);
}

[self fetchRandomDates];
}
4

1 回答 1

2

抛出异常的原因是:在 FecthresultControllern 中使用 insertNewObjectForEntityForName:@"RandomDate" inManagedObjectContext:managedObjectContext 而不是使用 enityForname: InManagedObjectContext ...

于 2012-11-09T11:18:46.070 回答