我不知道这里出错的地方是控制台的输出
2011-10-07 11:03:29.508 Golden Corral[2365:207] After whole process Name : Ggggggggggg
2011-10-07 11:03:29.513 Golden Corral[2365:207] After whole process Score : 27600
2011-10-07 11:03:29.515 Golden Corral[2365:207] Error : Error Domain=NSCocoaErrorDomain Code=1660 "The operation couldn’t be completed. (Cocoa error 1660.)" UserInfo=0x5566d60 {NSValidationErrorObject=<HighScore: 0x5563eb0> (entity: HighScore; id: 0x5555ed0 <x-coredata:///HighScore/tDAD877F0-0594-4E14-819F-AF5BDA4A38A82> ; data: {
PlayerName = Ggggggggggg;
TopTenScore = 27600;
}), NSValidationErrorKey=PlayerName, NSLocalizedDescription=The operation couldn’t be completed. (Cocoa error 1660.), NSValidationErrorValue=Ggggggggggg}
这是我保存数据的代码。我正在检查用户是否输入他的名字以获得高分,如果不是m取名,则匿名其他用户输入的内容。 Gdb显示用户名和highScore的正确值,但数据不存储在数据库中。
-(void)calculateHighScore
{
////////////////////////// Core Data Entries. ///////////////
HighScore *ScoreData = (HighScore*)[NSEntityDescription insertNewObjectForEntityForName:@"HighScore" inManagedObjectContext:self.managedObjectContext];
if ([txtName.text isEqualToString:@""] || [txtName.text length] == 0 )
{
NSLog(@"The Name :%@",txtName.text);
NSLog(@"%d",CountHighScore);
ScoreData.PlayerName = @"Anonymous";
ScoreData.TopTenScore = [NSNumber numberWithInt:CountHighScore];
NSLog(@"The Name :%@",ScoreData.PlayerName);
NSLog(@"%d",CountHighScore);
isWinner = NO;
levelCount ++;
}
else
{
NSLog(@"Befor Text assign highscore :%d",CountHighScore);
//CountHighScore = [txtName.text intValue];
ScoreData.PlayerName = txtName.text;
ScoreData.TopTenScore = [NSNumber numberWithInt:CountHighScore];
NSLog(@"The Name :%@",txtName.text);
NSLog(@"%d",CountHighScore);
isWinner = NO;
levelCount ++;
}
NSLog(@"After whole process Name : %@",ScoreData.PlayerName);
NSLog(@"After whole process Score : %d",CountHighScore);
// Code for inserting into DataBase
NSError *CorrectError;
if (![self.managedObjectContext save:&CorrectError])
{
// Handle the error...
NSLog(@"Error : %@",CorrectError);
}
}
答案 0 :(得分:1)
在评论对话中发现了答案。
在模型中创建新密钥时,可以对该属性的值设置限制。
在当前示例中,对键PlayerName
的值设置了限制:它的最大长度设置为8.
因此,当您将PlayerName
键的值设置为@"Ggggggggggg"
且长度超过8时,您将在向数据库提交更改时收到错误。只有在对对象进行了所有更改并希望通过提交更改将它们保存到数据库之后,才会检查所有限制。