一对多关系 CoreData Swift
One to many relationship CoreData Swift
核心数据在大多数情况下都很好用。当我先点击 name VC(Items)并点击 performSeque 到第二个 VC(Costs)时,我可以看到 costsName 和其他数据。但是当我在第一个 VC 中添加第二个名字时,我可以看到与名字相同的数据。
我正在尝试建立一对多关系。
我有 2 个数据模型:
import Foundation
import CoreData
@objc(Items)
class Items: NSManagedObject {
@NSManaged var count: NSNumber
@NSManaged var name: String
@NSManaged var cost: NSSet
}
import Foundation
import CoreData
@objc(Costs)
class Costs: NSManagedObject {
@NSManaged var costsDate: NSDate
@NSManaged var costsName: String
@NSManaged var costsValue: NSNumber
@NSManaged var account: Items
}
这是 addAccount
的(第一个 VC 的名称)保存操作:
@IBAction func saveButtonPressed(sender: UIBarButtonItem) {
let appDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
var managedObjectContext = appDelegate.managedObjectContext
let entityDescription = NSEntityDescription.entityForName("Items", inManagedObjectContext: managedObjectContext!)
let account = Items(entity: entityDescription!, insertIntoManagedObjectContext: managedObjectContext!)
account.name = cellOneNameTextField.text
if cellTwoCountTextField.text.isEmpty {
} else {
account.count = (cellTwoCountTextField.text).toInt()!
}
// Saving data
appDelegate.saveContext()
var request = NSFetchRequest(entityName: "Items")
var error:NSError? = nil
var results:NSArray = managedObjectContext!.executeFetchRequest(request, error: &error)!
self.navigationController?.popViewControllerAnimated(true)
}
这是 addCost
的保存操作:
@IBAction func saveButtonTapped(sender: UIBarButtonItem) {
// CoreData Access
let appDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
var managedObjectContext = appDelegate.managedObjectContext
let entityDescription = NSEntityDescription.entityForName("Costs", inManagedObjectContext: managedObjectContext!)
let cost = Costs(entity: entityDescription!, insertIntoManagedObjectContext: managedObjectContext!)
cost.costsName = cellThreeNoteTextField.text
cost.costsValue = (cellOnePriceTextField.text).toInt()!
cost.costsDate = datePicker.date
// Saving data
appDelegate.saveContext()
var request = NSFetchRequest(entityName: "Costs")
var error:NSError? = nil
var results:NSArray = managedObjectContext!.executeFetchRequest(request, error: &error)!
for res in results {
println(res)
}
delegate?.refreshTable()
self.navigationController?.popViewControllerAnimated(true)
}
我不知道你是否在某个地方做过,但是你的 Items
应该使用你的 Items
中的 cost
变量将一个 Count
对象附加到它自己。像 :
let account = Items(...)
let cost = Cost(...)
account.cost.addObject(cost)//and changing your var cost:NSSet into var cost:NSMutableSet
//then save Items
(没试过addObject,原理你懂的)
核心数据在大多数情况下都很好用。当我先点击 name VC(Items)并点击 performSeque 到第二个 VC(Costs)时,我可以看到 costsName 和其他数据。但是当我在第一个 VC 中添加第二个名字时,我可以看到与名字相同的数据。
我正在尝试建立一对多关系。
我有 2 个数据模型:
import Foundation
import CoreData
@objc(Items)
class Items: NSManagedObject {
@NSManaged var count: NSNumber
@NSManaged var name: String
@NSManaged var cost: NSSet
}
import Foundation
import CoreData
@objc(Costs)
class Costs: NSManagedObject {
@NSManaged var costsDate: NSDate
@NSManaged var costsName: String
@NSManaged var costsValue: NSNumber
@NSManaged var account: Items
}
这是 addAccount
的(第一个 VC 的名称)保存操作:
@IBAction func saveButtonPressed(sender: UIBarButtonItem) {
let appDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
var managedObjectContext = appDelegate.managedObjectContext
let entityDescription = NSEntityDescription.entityForName("Items", inManagedObjectContext: managedObjectContext!)
let account = Items(entity: entityDescription!, insertIntoManagedObjectContext: managedObjectContext!)
account.name = cellOneNameTextField.text
if cellTwoCountTextField.text.isEmpty {
} else {
account.count = (cellTwoCountTextField.text).toInt()!
}
// Saving data
appDelegate.saveContext()
var request = NSFetchRequest(entityName: "Items")
var error:NSError? = nil
var results:NSArray = managedObjectContext!.executeFetchRequest(request, error: &error)!
self.navigationController?.popViewControllerAnimated(true)
}
这是 addCost
的保存操作:
@IBAction func saveButtonTapped(sender: UIBarButtonItem) {
// CoreData Access
let appDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
var managedObjectContext = appDelegate.managedObjectContext
let entityDescription = NSEntityDescription.entityForName("Costs", inManagedObjectContext: managedObjectContext!)
let cost = Costs(entity: entityDescription!, insertIntoManagedObjectContext: managedObjectContext!)
cost.costsName = cellThreeNoteTextField.text
cost.costsValue = (cellOnePriceTextField.text).toInt()!
cost.costsDate = datePicker.date
// Saving data
appDelegate.saveContext()
var request = NSFetchRequest(entityName: "Costs")
var error:NSError? = nil
var results:NSArray = managedObjectContext!.executeFetchRequest(request, error: &error)!
for res in results {
println(res)
}
delegate?.refreshTable()
self.navigationController?.popViewControllerAnimated(true)
}
我不知道你是否在某个地方做过,但是你的 Items
应该使用你的 Items
中的 cost
变量将一个 Count
对象附加到它自己。像 :
let account = Items(...)
let cost = Cost(...)
account.cost.addObject(cost)//and changing your var cost:NSSet into var cost:NSMutableSet
//then save Items
(没试过addObject,原理你懂的)