【问题标题】:Connecting my NSManagedObject subclass with my ViewController.swift将我的 NSManagedObject 子类与我的 ViewController.swift 连接起来
【发布时间】:2016-06-21 15:24:56
【问题描述】:

概览

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

问题

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

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

}

我提前感谢你们。对不起,我完全是菜鸟。

【问题讨论】:

    标签: ios swift core-data nsmanagedobject


    【解决方案1】:

    由于您创建了NSManagedObject 的子类,您可以使用该子类而无需任何特殊步骤来“连接”它。它已经准备好了,它可以做任何由NSManagedObject 定义的事情以及在新子类中添加的任何事情。

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

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

    insertNewObjectForEntityForName(_:inManagedObjectContext:) 的调用将创建UsedInfo 的实例,但您需要添加as! UsedInfo 以明确您得到的结果。使用as! 可能很危险,但在这里也不错,因为如果向下转换失败,您希望立即了解,以便修复托管对象模型。

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

    entity1.product = one
    

    【讨论】:

    • 这正是我想要的!谢谢你,汤姆。我知道我的问题看起来很愚蠢,我只是不习惯 iOS 开发。
    猜你喜欢
    • 1970-01-01
    • 2020-08-25
    • 2016-09-08
    • 1970-01-01
    • 2011-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-26
    相关资源
    最近更新 更多