[英]How do I bind my Array Controller to my core data model?
I'm new to SWIFT programming and am trying to do a simple app to learn to use core data and bind it to display in an app. 我是SWIFT编程的新手,正在尝试制作一个简单的应用程序,以学习使用核心数据并将其绑定到应用程序中进行显示。 I've looked at loads of examples but all seem to be old. 我看过很多例子,但似乎都旧了。 I am working in XCode 9. 我正在使用XCode 9。
I started with a MacOS Cocoa app with Core Data. 我从带有Core Data的MacOS Cocoa应用开始。 I have a simple entity called "Workout" with 4 attributes date, seconds, sport and rpe. 我有一个名为“锻炼”的简单实体,具有4个属性,日期,秒,运动和rpe。
I then added a Array Controller under the view controller scene. 然后,我在视图控制器场景下添加了一个数组控制器。 I added a Table view which set the Array Controller as the data source. 我添加了一个Table视图,该视图将Array Controller设置为数据源。 I've added buttons to add and delete linking to array controllers add and remove methods. 我添加了用于添加和删除按钮的链接,这些链接指向数组控制器的add和remove方法。
I think all I need to do now is to bind the Array Controller to the managedObjectContext of my Core Data Model. 我认为我现在要做的就是将阵列控制器绑定到我的核心数据模型的managedObjectContext。 This is found in the AppDelegate. 这可以在AppDelegate中找到。 However when I select the Array Controller and go to Bindings and select Parameters the only options to bind to are "View Controller" and "Shared User Defaults Controller". 但是,当我选择“阵列控制器”并转到“绑定”并选择“参数”时,绑定到的唯一选项是“视图控制器”和“共享用户默认控制器”。 I've selected View Controller but cannot figure out the Model Key Path to link in to my data model. 我已经选择了View Controller,但是无法找出要链接到我的数据模型的Model Key Path。
I feel I must be missing something obvious. 我觉得我一定缺少明显的东西。 I feel there must either be a way to bind to the AppDelegate or a Model Key Path from the View Controller but I can't figure out either. 我觉得必须有一种方法可以绑定到AppDelegate或从视图控制器绑定到模型键路径,但我也找不到。 Any help much appreciated. 任何帮助,不胜感激。
In a storyboard based project there is no (binding) reference from a view controller to the AppDelegate
class. 在基于情节提要的项目中,没有从视图控制器到AppDelegate
类的(绑定)引用。
A solution is to add a property and override init?(coder
in the view controller 一种解决方案是添加属性并覆盖init?(coder
视图控制器中的init?(coder
@objc let managedObjectContext: NSManagedObjectContext
required init?(coder: NSCoder) {
self.managedObjectContext = (NSApp.delegate as! AppDelegate).persistentContainer.viewContext
super.init(coder: coder)
}
then bind the ManagedObjectContext
to ViewController
-> managedObjectContext
. 然后将ManagedObjectContext
绑定到ViewController
> managedObjectContext
。
In the Attribute Inspector of the array controller don't forget to set the Mode
to Entity Name
, insert the entity name and check Prepares Content
. 不要忘记在阵列控制器的属性检查器中将“ Mode
设置为“ Entity Name
,插入实体名称并选中“ Prepares Content
。
To fix [<Core_Data_Binding.ViewController 0x6080000c4600> valueForUndefinedKey:]: this class is not key value coding-compliant for the key managedObjectContext
change let managedObjectContext: NSManagedObjectContext
in vadian's code to @objc let managedObjectContext: NSManagedObjectContext
. 要修复[<Core_Data_Binding.ViewController 0x6080000c4600> valueForUndefinedKey:]: this class is not key value coding-compliant for the key managedObjectContext
更改的[<Core_Data_Binding.ViewController 0x6080000c4600> valueForUndefinedKey:]: this class is not key value coding-compliant for the key managedObjectContext
let managedObjectContext: NSManagedObjectContext
ManagedObjectContext let managedObjectContext: NSManagedObjectContext
vadian代码中的let managedObjectContext: NSManagedObjectContext
更改为@objc let managedObjectContext: NSManagedObjectContext
。 XCode 9 by default does not expose properties and methods to Objective C: The use of Swift 3 @objc inference in Swift 4 mode is deprecated? 默认情况下,XCode 9不会向目标C公开属性和方法: 不赞成在Swift 4模式下使用Swift 3 @objc推论吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.