我不是CoreData的新手,我有一个用于初始化所有对象的类,将它们设置在数组中,并使用.append从pickerView数组追加到TableView数组。当然,终止应用程序后,将清除tableview数组。我正在尝试使用核心数据进行保存,但tableView现在返回了代码中的每个对象。如何选择一个对象并将其设置为tableView使用的数组。预先感谢
SavedRun是我的实体
import Foundation
import CoreData
extension SavedRun {
@nonobjc public class func fetchRequest() -> NSFetchRequest<SavedRun> {
return NSFetchRequest<SavedRun>(entityName: "SavedRun")
}
@NSManaged public var formulaTitle: String?
@NSManaged public var formulaNumber: String?
@NSManaged public var base: String?
@NSManaged public var baseValue: Double
@NSManaged public var vit: String?
@NSManaged public var vitValue: Double
@NSManaged public var min: String?
@NSManaged public var minValue: Double
@NSManaged public var verdNat: String?
@NSManaged public var verdNatValue: Double
}
然后在我的init课上。
import UIKit
class AllFormulasTest {
var pickerArr = [SavedRun]()
static var tableArr : [SavedRun] = []
init() {
let F1354670 = SavedRun(context: PersistenceService.context)
F1354670.formulaNumber = "F1354670"
F1354670.formulaTitle = "test formula title"
F1354670.base = "Base name"
F1354670.baseValue = 251.90
F1354670.vit = "Vit name"
F1354670.vitValue = 50.38
F1354670.min = "Min name"
F1354670.minValue = 45.34
F1354670.verdNat = "Verd name"
F1354670.verdNatValue = 16.79
// I have about 200 formulas like the one above I use for my work.
// pickerArr is used for my pickerView then appened to tableArr array of what needs to be made.
pickerArr = [F1354670, F1354670, F1354670, F1354670, F1354670]
// pickerArr returns all the ones created (not just 'F1354670') and works great.
//this array needs to be what is shown on the tableView
AllFormulas.tableArr = [] as? [SavedRun] ?? []
// but.. I think doing it this way is acutally saving F1354670 into 'SavedRun'
// which causes tableArr array to show them aswell
}
}
我如何才能使公式像数据库一样,直到从pickerArr中选择出来才显示在tableArr中。
先谢谢您