我正在开发一个使用CoreData的应用。
我是强大的数字并检索它们,但是当我存储多个字段时,它只显示带有数字的最后一个字段,但是显示所有其他字段中的0。以下是我的代码。
存储它们......
let CWConvert = Double(CWeight.text!)
storeTranscription18CW(pageText: (CWConvert)!, textFileUrlString: "cWeight")
savePlus += 1
let TWConvert = Double(TWeight.text!)
storeTranscription18TW(pageText: (TWConvert)!, textFileUrlString: "tWeight")
和...
func getContext () -> NSManagedObjectContext {
_ = UIApplication.shared.delegate as! AppDelegate
return DataController().managedObjectContext
}
func storeTranscription18CW (pageText: Double, textFileUrlString: String) {
let context = getContext()
//retrieve the entity that we just created
let entity = NSEntityDescription.entity(forEntityName: "TextInputs", in: context)
let transc = NSManagedObject(entity: entity!, insertInto: context)
// set the entity values
transc.setValue(pageText, forKey: "cWeight")
//save the object
do {
try context.save()
print("saved!")
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
} catch {
}
}
func storeTranscription18TW (pageText: Double, textFileUrlString: String) {
let contexta = getContext()
//retrieve the entity that we just created
let entitya = NSEntityDescription.entity(forEntityName: "TextInputs", in: contexta)
let transc = NSManagedObject(entity: entitya!, insertInto: contexta)
// set the entity values
transc.setValue(pageText, forKey: "tWeight")
//save the object
do {
try contexta.save()
print("saved!")
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
} catch {
}
}
并且要追溯。
func getTranscriptions18CW () {
//create a fetch request, telling it about the entity
let fetchRequesta: NSFetchRequest<TextInputs> = TextInputs.fetchRequest()
do {
//go get the results
let searchResults18CW = try getContext().fetch(fetchRequesta)
for transa in searchResults18CW as [NSManagedObject] {
if let resulta = transa.value(forKey: "cWeight") {
if let stra = resulta as? String {
CWeight.text = stra
}else {
CWeight?.text = "\(resulta)"
}
}
}
//get the Key Value pairs (although there may be a better
}catch {
print("Error with request: \(error)")
}
}
func getTranscriptions18TW () {
//create a fetch request, telling it about the entity
let fetchRequest: NSFetchRequest<TextInputs> = TextInputs.fetchRequest()
do {
//go get the results
let searchResults18TW = try getContext().fetch(fetchRequest)
for trans in searchResults18TW as [NSManagedObject] {
if let result = trans.value(forKey: "tWeight") {
if let str = result as? String {
TWeight.text = str
}else {
TWeight?.text = "\(result)"
}
}
//get the Key Value pairs (although there may be a better way to do that...
}
}catch {
print("Error with request: \(error)")
}
}
我尝试了不同的名字,但得到的相同它只显示最后一个作为实数,如果我突破最后一个然后第一个显示实际值。
答案 0 :(得分:0)
尝试更多类似的内容进行保存,以便将两者保存在同一行中。
func storeTranscription18TW () {
let contexta = getContext()
//retrieve the entity that we just created
let entitya = NSEntityDescription.entity(forEntityName: "TextInputs", in: contexta)
let transc = NSManagedObject(entity: entitya!, insertInto: contexta)
// set the entity values
transc.setValue(CWConvert, forKey: "cWeight") // Note the change from pageText
transc.setValue(TWConvert, forKey: "tWeight") // Note the change from pageText
//save the object
do {
try contexta.save()
print("saved!")
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
} catch {
}
}
然后,当你做一个get,他们都将在同一个函数中完成。