gpt4 book ai didi

iOS 核心数据更新 : changes not saved

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:45:05 24 4
gpt4 key购买 nike

我正在尝试在我的 iPad 应用程序中使用 Core Data。

我有一个像这样的模型:

客户.h

#import <CoreData/CoreData.h>
@interface Customer : NSManagedObject
@property (nonatomic,strong) NSString *name;
@end

客户.m

#import "Customer.h"
@implementation Customer
@synthesize name;
@end

TableView Controller 显示数据库中的所有客户。通过选择客户,启动详细信息 View 推送 segue 并设置客户:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.

CustomerDetailViewController *detailViewController = (CustomerDetailViewController*) [segue destinationViewController];
detailViewController.customer = [customers objectAtIndex:self.tableView.indexPathForSelectedRow.row];
}

在详细 View Controller 中,如果发生更改,操作保存应该保存客户:

- (IBAction)save:(id)sender 
{
AppDelegate *delegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *moc = [delegate managedObjectContext];
self.customer.name = self.nameTextField.text;
NSError *error;
if (![moc save:&error])
{
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}
}

虽然更改了名称,但未保存更改:虽然没有出现错误(错误为 nil,未显示日志)...

另外:

  BOOL customerHasChanges = [self.customer hasChanges];
BOOL mocHasChanges = [moc hasChanges];

都是假的!

但是插入新实体工作正常:

Customer *customer = [NSEntityDescription insertNewObjectForEntityForName:@"Customer" inManagedObjectContext:self.managedObjectContext];
[customer setValue:@"Mars Inc." forKey:@"name"];
NSError *error;
[self.managedObjectContext save:&error];

最佳答案

您的实现文件中有错误。使用 @dynamic 而不是 @synthesize

关于iOS 核心数据更新 : changes not saved,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19477604/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com