Iphone ios Coredata-我的保存按钮不工作
我正在尝试为我的CoreData项目创建一个保存按钮,通常我没有问题。但这次我没有看到它保存到SQL文件(在FireFox中使用SQLite),也无法在UITableView中提取数据 没有错误或警告。谁能帮我解决星期天下午(新西兰时间)乌云密布的问题Iphone ios Coredata-我的保存按钮不工作,iphone,ios,core-data,save,Iphone,Ios,Core Data,Save,我正在尝试为我的CoreData项目创建一个保存按钮,通常我没有问题。但这次我没有看到它保存到SQL文件(在FireFox中使用SQLite),也无法在UITableView中提取数据 没有错误或警告。谁能帮我解决星期天下午(新西兰时间)乌云密布的问题 在上下文中添加对象后,需要保存上下文。尝试我的代码一旦完成删除、更新或插入,您总是需要保存上下文。别忘了:-)@JeffKranenburg。 - (IBAction)saveDataAction:(id)sender { //Instantia
在上下文中添加对象后,需要保存上下文。尝试我的代码一旦完成删除、更新或插入,您总是需要保存上下文。别忘了:-)@JeffKranenburg。
- (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);
}
- (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);
}