Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
160 views
in Technique[技术] by (71.8m points)

ios - CoreData errors for simple adding to database

Edit: The error messages that I am trying to figure out are on the right side of the code listed as comments!!!

Xcode keeps trying to suggest alternatives and refusing to let me build. The 3 are listed in comment tags on the right side of the code. What do they mean? I am very new to using core data and really hoping to see what I am doing wrong, just using username as an example (as I know I shouldn't be doing so)!

- (IBAction)saveData:(id)sender {
    NSManagedObjectContext *cxt = [self managedObjectContext];       //instance method managedObjectContext not found
    NSManagedObject *newContacts = [NSEntityDescription insertNewObjectForEntityForName:@"Contacts" inManagedObjectContext:cxt];
    [newContacts setValue:self.username.text forKey:@"username"];          //insert NSManagedObjectModel instead of NSManagedObject...claims that it is undeclared?
    username.text = @"";

    NSManagedObject *newContacts = [NSEntityDescription insertNewObjectForEntityForName:@"Contacts" inManagedObjectContext:cxt];     //trying to replace NSEntityDescription with KSecAttrDescription...claims that it is undeclared      
    [newContacts setValue:self.password.text forKey:@"password"];
    password.text = @"";

    NSError *err;
    if (![cxt save:&err]) {
        NSLog(@"An error has occured: %@", [err localizedDescription]);      //Instance Method -save not found
    }           

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

CoreData code looks good, but you are using NSManagedObject *newContacts twice, just use another variable name or remove NSManagedObject * for the second object.

Also i would recommend you to make classes for your objects to have a direct access to variables instead of [obj setValue:value forKey:@"key"];

And of course it would be nice to see the error output.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...