在Core Data模型和Cocoa Error 1660中存储int32

时间:2010-12-03 00:23:18

标签: iphone core-data

编辑:我能够通过更改NSLog来呈现实际的int而不是指针来修复表示事物。

  

NSLog(@“id:%i”,[[info valueForKey:@“idQ”] intValue]);

仍有Cocoa错误1660来解决??

我正在使用Core Data进行我的第一次测试,并正在使用它(http://www.raywenderlich.com/934/core-data-tutorial-getting-started)教程。

然后我尝试了自己的测试,并且有些成功。但是,int32(因为教程只有32)导致一个奇怪的数字。我怀疑它与NSNumber有关。 Cocoa错误1660我认为输出中的长数字有一些东西(输入= 1和输出= 95518976)。我想这是一个成为指针的问题,需要正确呈现,但错误呢。

有人可以给我一个提示如何解决这个问题吗?

- (void)testingDBmodel {

NSLog(@">>testingDBmodel<<");

//=================DATABASE===================//
// id
// qDiff
// question
// qRightAnswer
// qWrongAnswer1
// qWrongAnswer2
// qNr
// qRegDate

if (managedObjectContext == nil) { managedObjectContext = [(FamQuiz_v2AppDelegate *)
                                [[UIApplication sharedApplication] delegate] managedObjectContext]; }


NSManagedObjectContext *context = [self managedObjectContext];
NSManagedObject *famQuizInfo = [NSEntityDescription
                                   insertNewObjectForEntityForName:@"questions" 
                                   inManagedObjectContext:context];
[famQuizInfo setValue:[NSNumber numberWithInt:1] forKey:@"idQ"];
[famQuizInfo setValue:@"qDiff1" forKey:@"qDiff"];
[famQuizInfo setValue:@"question1" forKey:@"question"];
[famQuizInfo setValue:@"qRightAnswer1" forKey:@"qRightAnswer"];
[famQuizInfo setValue:@"qWrongAnswer1_1" forKey:@"qWrongAnswer1"];
[famQuizInfo setValue:@"qWrongAnswer2_2" forKey:@"qWrongAnswer2"];
[famQuizInfo setValue:@"999" forKey:@"qNr"];
[famQuizInfo setValue:[NSDate date] forKey:@"qRegDate"];

NSError *error;
if (![context save:&error]) {
    NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}


//==========READ DATABASE==============//
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription 
                               entityForName:@"questions" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
for (NSManagedObject *info in fetchedObjects) {
    NSLog(@"id: %i", [info valueForKey:@"idQ"]);
    NSLog(@"qDiff: %@", [info valueForKey:@"qDiff"]);
    NSLog(@"question: %@", [info valueForKey:@"question"]);
    NSLog(@"qRightAnswer: %@", [info valueForKey:@"qRightAnswer"]);
    NSLog(@"qWrongAnswer1: %@", [info valueForKey:@"qWrongAnswer1"]);
    NSLog(@"qWrongAnswer2: %@", [info valueForKey:@"qWrongAnswer2"]);
    NSLog(@"qNr: %@", [info valueForKey:@"qNr"]);
    NSLog(@"qRegDate: %@", [info valueForKey:@"qRegDate"]);
}        
[fetchRequest release];

}

输出:

  

2010-12-03 01:11:54.097 Test_v2 [11177:207]&gt;&gt; testingDBmodel&lt;&lt;
  2010-12-03 01:11:54.104 Test_v2 [11177:207]哎呀,无法保存:操作无法完成。 (可可错误1660.)
  2010-12-03 01:11:54.112 Test_v2 [11177:207] id:1
  2010-12-03 01:11:54.112 Test_v2 [11177:207] qDiff:qDiff1
  2010-12-03 01:11:54.113 Test_v2 [11177:207]问题:问题1   2010-12-03 01:11:54.113 Test_v2 [11177:207] qRightAnswer:qRightAnswer1
  2010-12-03 01:11:54.114 Test_v2 [11177:207] qWrongAnswer1:qWrongAnswer1_1
  2010-12-03 01:11:54.114 Test_v2 [11177:207] qWrongAnswer2:qWrongAnswer2_2
  2010-12-03 01:11:54.114 Test_v2 [11177:207] qNr:999
  2010-12-03 01:11:54.116 Test_v2 [11177:207] qRegDate:2010-12-03 01:11:54 +0100

1 个答案:

答案 0 :(得分:13)

可可错误1660 =约束错误。检查数据库中的某些数据字段是否受限制。 我得到了这个错误并修正了我当我试图在一个限制为10个字符的字段中放置20个字符时...