以编程方式添加到核心数据实体(如 IB 绑定“添加”)
我有一个核心数据模型,其中有一个名为 clients 的实体,该实体由以下属性组成:
如果我单击“添加客户端”按钮并弹出以下窗口:
以编程方式同时为每个属性添加新条目的正确方法是什么(类似于绑定 IB“添加”按钮与 NSArrayController 一起使用的方式)并让它们出现在“添加客户端”窗口的文本字段中进行编辑吗? “添加客户端”窗口中的文本字段绑定到客户端实体的相应属性(仍缺少一两个属性)。我目前拥有的代码是:
- (IBAction)addNewClient:(id)sender;
{
[addClientsWindow makeKeyAndOrderFront:self];
//NSManagedObjectContext *clientsMoc= [clientsController managedObjectContext];
//[clientsMoc addObject:[clientsMoc newObject]];
[clientsController addObject:[clientsController newObject]];
}
它适用于该项目中的其他实体,但不适用于 客户端 ,因为我添加了关系类型(它在控制台中引发了 KVC 错误)。我想这是因为我正在处理 NSArrayController
而不是 NSManagedObjectContext
但注释掉的代码对我不起作用。此尝试是从上一个问题开始的,因为问题已改变了很多,我正在努力实施所给出的建议。我真的需要一个好的起点,而苹果开发文档并没有帮助我理解这一点。
提前致谢!
---- 更新 ----
我在这个问题中解释得不好吗?我是新来的,但认为可能会尝试给出答案。由于某种原因,谷歌搜索这件事很困难。我可能通过不相关的谷歌搜索找到了类似于解决方案的东西。幸运的是,它与此相关。
- (IBAction)addNewClient:(id)sender;
{
[addClientsWindow makeKeyAndOrderFront:self];
NSManagedObjectContext *clientsMoc= [clientsController managedObjectContext];
NSManagedObject *clientsEntity = [NSEntityDescription
insertNewObjectForEntityForName:@"Clients"
inManagedObjectContext:clientsMoc];
[clientsEntity setValue:@"name" forKey:@"clientName"];
[clientsEntity setValue:@"company" forKey:@"clientCompany"];
[clientsEntity setValue:@"address" forKey:@"clientAddress"];
[clientsEntity setValue:@"11111111" forKey:@"clientLandline"];
[clientsEntity setValue:@"[email protected]" forKey:@"clientEmail"];
}
这为客户实体创建了一个全新的条目 - 我没有意识到我必须为每个属性执行单独的值。但 KVC 错误仍然存在,我根本找不到解决方案。苹果开发文档实际上对这个很有帮助, 此处,并将 NSBindingDebugLogLevel 1
添加到“启动时传递的参数”列表中,为我提供了导致问题的确切原因的详细信息。它是一个到 NSTableColumn
的旧绑定,尚未更新。
I have a core data model with an entity called clients that is made up of the following attributes:
If I click on an 'add client' button and bring up the following window:
what would be the correct method to programmatically add a new entry for each property simultaneously (in a similar fashion to how a bound IB 'add' button would work with an NSArrayController
) and have them appear in the textfields of the 'add client' window to edit? The textfields in the 'add client' window are bound to the corresponding properties (with one or two still missing) of the client entity. The code I have at the moment is:
- (IBAction)addNewClient:(id)sender;
{
[addClientsWindow makeKeyAndOrderFront:self];
//NSManagedObjectContext *clientsMoc= [clientsController managedObjectContext];
//[clientsMoc addObject:[clientsMoc newObject]];
[clientsController addObject:[clientsController newObject]];
}
Which worked for other entities in this project but isn't working for client since I added relationship types (it throws up KVC errors in the console). I imagine it's because I am addressing the NSArrayController
rather than the NSManagedObjectContext
but the commented out code isn't working for me. This attempt follows on from a previous question as the question has changed a lot and I'm struggling to implement the advice given. I really need a good starting point and the apple dev docs aren't helping me make sense of this.
Thanks in advance!
---- Update ----
Am I explaining things badly in this question? I'm new here but thought there might be an attempt at an answer. Googling this was hard for some reason. I may have found something similar to a solution through an unrelated Google search. Luckily it was relevant to this.
- (IBAction)addNewClient:(id)sender;
{
[addClientsWindow makeKeyAndOrderFront:self];
NSManagedObjectContext *clientsMoc= [clientsController managedObjectContext];
NSManagedObject *clientsEntity = [NSEntityDescription
insertNewObjectForEntityForName:@"Clients"
inManagedObjectContext:clientsMoc];
[clientsEntity setValue:@"name" forKey:@"clientName"];
[clientsEntity setValue:@"company" forKey:@"clientCompany"];
[clientsEntity setValue:@"address" forKey:@"clientAddress"];
[clientsEntity setValue:@"11111111" forKey:@"clientLandline"];
[clientsEntity setValue:@"[email protected]" forKey:@"clientEmail"];
}
This created a full new entry for the clients entity - I didn't realise I would have to do an individual value for each property. The KVC errors continued though and I couldn't find a solution at all. Apple dev docs were actually helpful on this one, here, and adding NSBindingDebugLogLevel 1
to the “Arguments to be passed at launch” list gave me details of exactly what was causing the problem. It was an old binding to an NSTableColumn
that hadn't been updated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能是我所追求的解决方案:
这为客户实体创建了一个全新的条目 - 我没有意识到我必须为每个属性执行单独的值。但 KVC 错误仍然存在,我根本找不到解决方案。苹果开发文档实际上对这一点很有帮助, 这里,并将
NSBindingDebugLogLevel 1
添加到“启动时传递的参数”列表中,为我提供了导致问题的确切原因的详细信息。它是一个到NSTableColumn
的旧绑定,尚未更新。This may be the solution I was after:
This created a full new entry for the clients entity - I didn't realise I would have to do an individual value for each property. The KVC errors continued though and I couldn't find a solution at all. Apple dev docs were actually helpful on this one, here, and adding
NSBindingDebugLogLevel 1
to the “Arguments to be passed at launch” list gave me details of exactly what was causing the problem. It was an old binding to anNSTableColumn
that hadn't been updated.