ios - 将我的 NSManagedObject 子类与我的 ViewController.swift 连接起来

标签 ios swift core-data nsmanagedobject

概述

这可能是一个极其愚蠢/简单的问题,但它似乎让我陷入了困境。我根据 StackOverflow 上同事的建议为我的实体“UsedInfo”创建了一个 NSManagedObject 子类。我的最终目标是使用这个子类将用户信息发送到CoreData,然后检索它。

问题

问题是我无法弄清楚如何将我的新文件“UsedInfo+CoreDataProperties.swift”与我的“ViewController.swift”文件一起使用,这是我的 TextFields 连接的地方。您将在下面找到相关代码。

ViewController.swift 代码(SaveButton)

@IBAction func saveButton(sender: AnyObject) {
    let appDel: AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
    let context:NSManagedObjectContext = appDel.managedObjectContext
    let managedContext:NSManagedObjectContext = appDel.managedObjectContext
    let entity1 = NSEntityDescription.insertNewObjectForEntityForName("UsedInfo", inManagedObjectContext:context) as NSManagedObject

    let one = pickerTextField.text
    let two = modelName.text
    let three = serialNo.text
    let four = YOM.text
    let five = engineHours.text
    let six = locationOfMachine.text

    entity1.setValue(one, forKey: "product")
    entity1.setValue(two, forKey:"modelName")
    entity1.setValue(three, forKey:"serialNo")
    entity1.setValue(four, forKey:"yom")
    entity1.setValue(five, forKey:"engineHours")
    entity1.setValue(six, forKey:"location")
    print(entity1.valueForKey("product"))
    print(entity1.valueForKey("modelName"))
    print(entity1.valueForKey("serialNo"))
    print(entity1.valueForKey("yom"))
    print(entity1.valueForKey("engineHours"))


    do {
        try context.save()
    }
    catch {
        print("error")
    }


}

“UsedInfo+CoreDataProperties.swift”代码

import Foundation
import CoreData

extension UsedInfo {

@NSManaged var engineHours: String?
@NSManaged var location: String?
@NSManaged var modelName: String?
@NSManaged var product: String?
@NSManaged var serialNo: String?
@NSManaged var yom: String?

}

“UsedInfo.swift”代码

import Foundation
import CoreData
class UsedInfo: NSManagedObject {

    //Insert code here to add functionality to your managed object subclass

}

我提前感谢你们。我为我的菜鸟行为感到抱歉。

最佳答案

由于您创建了 NSManagedObject 的子类,您可以使用该子类而无需任何特殊步骤来“连接”它。它已经准备就绪,可以执行 NSManagedObject 定义的任何操作以及添加到新子类中的任何操作。

对于 Core Data,您首先要将创建新实例的代码更改为类似

let entity1 = NSEntityDescription.insertNewObjectForEntityForName("UsedInfo", inManagedObjectContext:context) as NSManagedObject as! UsedInfo

调用 insertNewObjectForEntityForName(_:inManagedObjectContext:) 将创建一个 UsedInfo 的实例,但您需要将 添加为! UsedInfo 来明确说明你得到的是什么。使用 as! 可能很危险,但这并不是一个坏主意,因为如果此向下转换失败,您希望立即知道,以便您可以修复您的托管对象模型。

在那之后,entity1 是您的新 UsedInfo 类的一个实例。您可以在下面的代码中使用在 UsedInfo 上声明的属性。例如,

entity1.product = one

关于ios - 将我的 NSManagedObject 子类与我的 ViewController.swift 连接起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37948521/

相关文章:

Ios Swift 日期从时区转换

ios - 如何禁用导航栏中的后退按钮

ios - 无法将两个 AlertController 放在同一个 VC 上

ios - '+entityForName : nil is not a legal NSManagedObjectContext parameter searching for entity name

Cocoa CoreData 和非基于文档的应用程序

ios - NSURL - 获取错误消息

ios - 如何将任何语言的日期转换为特定语言

objective-c - Corona应用程序如何支持不同的设备目标?

ios - 使用 sprites velocity (swift) flappy bird 游戏旋转 Sprite

ios - swift 2 : Populating a TableView with two CoreData entities