【发布时间】: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