开发者

Storing int32 in Core Data model and Cocoa Error 1660

Edit: I was able to fix the presentation thing by changing the NSLog to present the actual int rather than the pointer.

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

Still have the Cocoa error 1660 to solve??

I am doing my first tests with Core Data and is using this (http://www.raywenderlich.com/934/core-data-tutorial-getting-started) tutorial.

I have then tried my own tests and is somewhat successful. However, the int32 (only 32 because of the tutorial) is resulting in a strange number. I do suspect it has something to do with NSNumber. The Cocoa error 1660 i believe has something todo with to long number in the output (input = 1 and output = 95518976). I guess this is a question of being a pointer and needs to present correctly, but what about the error.

Could someone nice give me a hint how to fix this?

- (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
                                   in开发者_运维百科sertNewObjectForEntityForName:@"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];

}

The output:

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 error 1660 = a constraint error. Check if not some of the data fields in your database is constrained. i got this error and fixed it when I notieced that I was trying to put 20 characters in a field that had a limit of 10 characters...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜