ios核心数据 - 我的保存按钮不起作用

时间:2012-11-18 04:23:12

标签: iphone ios core-data save

我正在尝试为我的CoreData项目创建一个保存按钮,通常我没有任何问题。但这次我没有看到它保存到SQL文件(在FireFox中使用SQLite),也不能将数据拉回到UITableView中。

没有错误或警告。周日下午(新西兰时间)头上有人可以帮助我吗?

- (IBAction)saveDataAction:(id)sender
{
//Instantiating the Entity with a pointer and giving it a mission.
NSString *details = @"Details";
NSManagedObject *newObject = [NSEntityDescription insertNewObjectForEntityForName:details inManagedObjectContext:[self managedObjectContext]];

int yearOfBirth = [_yearTxtField.text intValue]; //Converting the value from a String into an int
NSNumber *year = [NSNumber numberWithInt:yearOfBirth]; //Converting the int into a NSNumber

//Setting each textField value to an attribute in the Entity.
[newObject setValue:_nameTxtField.text forKey:@"name"];
[newObject setValue:_streetTxtField.text forKey:@"streetName"];
[newObject setValue:_cityTxtField.text forKey:@"cityName"];
[newObject setValue:year forKey:@"yearOfBirth"];

//This log just shows what has been added to the database.
//NSLog(@"Details Save:\n\n Name: %@\n StreetName: %@\n CityName: %@\n Year of birth: %@", details.name, details.streetName, details.cityName, details.yearOfBirth);
}

1 个答案:

答案 0 :(得分:0)

- (IBAction)saveDataAction:(id)sender
{
     //Instantiating the Entity with a pointer and giving it a mission.
    NSString *details = @"Details";
    NSManagedObjectContext *context = [self managedObjectContext];
    NSManagedObject *newObject = [NSEntityDescription insertNewObjectForEntityForName:details inManagedObjectContext:context];

    int yearOfBirth = [_yearTxtField.text intValue]; //Converting the value from a String into an int
    NSNumber *year = [NSNumber numberWithInt:yearOfBirth]; //Converting the int into a NSNumber

    //Setting each textField value to an attribute in the Entity.
    [newObject setValue:_nameTxtField.text forKey:@"name"];
    [newObject setValue:_streetTxtField.text forKey:@"streetName"];
    [newObject setValue:_cityTxtField.text forKey:@"cityName"];
    [newObject setValue:year forKey:@"yearOfBirth"];

    NSError *error;
    if (![context save:&error]) {
        NSLog(@"Whoops couldn't save new object");
    }
    //This log just shows what has been added to the database.
    //NSLog(@"Details Save:\n\n Name: %@\n StreetName: %@\n CityName: %@\n Year of birth: %@", details.name, details.streetName, details.cityName, details.yearOfBirth);
}