简体   繁体   English

如何将阵列控制器绑定到核心数据模型?

[英]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.

相关问题 在访问核心数据模型之前,如何确保已进行所有网络调用? - How do I ensure that all network calls have been made before accessing my core data model? 我的应用程序中需要核心数据吗? - Do i need core data in my app? 如何在Cloudkit上创建此数据/模型以在我的应用程序中显示它? - How do I create this data/model on Cloudkit to display it in my app? 如何将数据从视图控制器传递到我的弹出视图控制器 (swift/ios) - How do I pass data from a View controller into my pop up view controller (swift/ios) 在我的根控制器中,如何检查是否有子控制器? - In my root controller, how do I check if there's a child controller? 如何让我的核心数据实体的属性在 objective-c 单元测试中可见 - How do I get my Core Data entity's attributes to be visible in objective-c unit tests 我是否正确使用数据模型? 如何为过滤器页面组织代码? - Am I using a data model properly? How do I organize my code for a filter page? 如何将 firestore 文档 ID 添加到我的模型并随后添加到数组? - How do I add firestore document Id to my model and subsequently to the array? 当我的核心数据存储已从后台扩展更新时,如何刷新我的 SwiftUI 视图? - How do I refresh my SwiftUI view when my core data store has been updated from a background extension? 如果我的 model object 的属性是自定义 object 的数组,我如何让用户 Z9516DFB15F51C7EEDBBED4 到它? - If a property of my model object is an array of a custom object, how do I let users append objects to it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM