I dont know where m getting wrong here is the output of console
我不知道这里出错的地方是控制台的输出
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}
and here is my code of saving data. I am checking whether the user enteres his name for highscore or not, if not m taking name Anonymous else what the user has entered. Gdb shows the correct value of both user name and highScore but data is not storing in database.
这是我保存数据的代码。我正在检查用户是否输入他的名字以获得高分,如果不是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);
}
}
1 个解决方案
#1
1
Answer was found during conversation in comments.
在评论的对话中发现了答案。
When you are creating new key in your model you can set restrictions to the values of that property.
在模型中创建新密钥时,可以对该属性的值设置限制。
In current example restrictions were set on values of key PlayerName
: maximum length of it was set to 8.
在当前示例中,对关键PlayerName的值设置了限制:其最大长度设置为8。
So when you set value of PlayerName
key to @"Ggggggggggg"
that has length more then 8 you will receive error when committing changes to the database. All restrictions are checked only after your have made all the changes to objects and want to save them to your database by committing changes.
因此,当您将PlayerName键的值设置为长度大于8的@“Ggggggggggg”时,您将在向数据库提交更改时收到错误。只有在对对象进行了所有更改并希望通过提交更改将它们保存到数据库之后,才会检查所有限制。
#1
1
Answer was found during conversation in comments.
在评论的对话中发现了答案。
When you are creating new key in your model you can set restrictions to the values of that property.
在模型中创建新密钥时,可以对该属性的值设置限制。
In current example restrictions were set on values of key PlayerName
: maximum length of it was set to 8.
在当前示例中,对关键PlayerName的值设置了限制:其最大长度设置为8。
So when you set value of PlayerName
key to @"Ggggggggggg"
that has length more then 8 you will receive error when committing changes to the database. All restrictions are checked only after your have made all the changes to objects and want to save them to your database by committing changes.
因此,当您将PlayerName键的值设置为长度大于8的@“Ggggggggggg”时,您将在向数据库提交更改时收到错误。只有在对对象进行了所有更改并希望通过提交更改将它们保存到数据库之后,才会检查所有限制。