gpt4 book ai didi

iphone - 在Core Data模型中存储int32和Cocoa错误1660

转载 作者:行者123 更新时间:2023-12-03 20:19:16 26 4
gpt4 key购买 nike

编辑:我能够通过更改 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] >>testingDBmodel<<
2010-12-03 01:11:54.104 Test_v2[11177:207] Whoops, couldn't save: The operation couldn’t be completed. (Cocoa error 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] question: question1
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

最佳答案

Cocoa 错误 1660 = 约束错误。检查数据库中的某些数据字段是否受到限制。当我注意到我试图在限制为 10 个字符的字段中放入 20 个字符时,我收到了此错误并修复了该错误...

关于iphone - 在Core Data模型中存储int32和Cocoa错误1660,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4341390/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com