2012-02-17 2 views
0

저는 초보자이며 처음부터 코어 데이터를 학습합니다. 나는 coredatamodel을 설정 한 다음 NSManagedObject 클래스를 생성했습니다. 그런 다음 응용 프로그램 대리인에서 일부 테스트 데이터를 삽입하려고했습니다. 그러나 제대로 작동하지 않았습니다. 마지막 데이터 만 삽입되었습니다. 배치해야 할 곳코어 데이터를 사용하여 여러 테스트 데이터 추가

[self saveContext];

모든 개체 사이에? "applicationWillTerminate"메서드에서 saveContext 메서드가 호출되므로 마지막 항목이 저장됩니다. (맞습니까?)

NSManagedObjectContext *context = [self managedObjectContext]; 

Vocabulary *vocabulary = [NSEntityDescription 
            insertNewObjectForEntityForName:@"Vocabulary" 
            inManagedObjectContext:context]; 

vocabulary.word = @"iPhone"; 
vocabulary.definition = @"better than Android"; 
vocabulary.level = @"beginner"; 

vocabulary.word = @"iPhone3gs"; 
vocabulary.definition = @"better than 3"; 
vocabulary.level = @"intermediate"; 

vocabulary.word = @"iPhone4"; 
vocabulary.definition = @"better than 3gs"; 
vocabulary.level = @"advanced"; 

vocabulary.word = @"iPhone4s"; 
vocabulary.definition = @"better than 4"; 

vocabulary.word = @"iPhone4s"; 
vocabulary.definition = @"64 is better than 32"; 
vocabulary.level = @"advanced"; 

답변

0

삽입 한 각 어휘 객체에 대해 새 엔티티를 삽입해야합니다. 이렇게 :

Vocabulary *vocabulary = nil; 
NSManagedObjectContext *context = [self managedObjectContext]; 

vocabulary = [NSEntityDescription insertNewObjectForEntityForName:@"Vocabulary" inManagedObjectContext:context]; 
vocabulary.word = @"iPhone"; 
vocabulary.definition = @"better than Android"; 
vocabulary.level = @"beginner"; 

vocabulary = [NSEntityDescription insertNewObjectForEntityForName:@"Vocabulary" inManagedObjectContext:context]; 
vocabulary.word = @"iPhone3gs"; 
vocabulary.definition = @"better than 3"; 
vocabulary.level = @"intermediate"; 

vocabulary = [NSEntityDescription insertNewObjectForEntityForName:@"Vocabulary" inManagedObjectContext:context]; 
vocabulary.word = @"iPhone4"; 
vocabulary.definition = @"better than 3gs"; 
vocabulary.level = @"advanced"; 

vocabulary = [NSEntityDescription insertNewObjectForEntityForName:@"Vocabulary" inManagedObjectContext:context]; 
vocabulary.word = @"iPhone4s"; 
vocabulary.definition = @"better than 4"; 

vocabulary = [NSEntityDescription insertNewObjectForEntityForName:@"Vocabulary" inManagedObjectContext:context]; 
vocabulary.word = @"iPhone4s"; 
vocabulary.definition = @"64 is better than 32"; 
vocabulary.level = @"advanced"; 
+0

감사합니다 Tim. 그것은 작동합니다! – Vector

관련 문제