【发布时间】:2017-02-08 00:56:05
【问题描述】:
我正在尝试将数字放入核心数据中,然后将它们取回。我已经可以使用字符串进行此操作,并且一切正常,但是尝试使用双打来进行操作似乎可以存储它们但不能检索它们。下面是我的代码。希望有人能帮忙,如果可以的话提前谢谢。
要检索...
func getTranscriptions18CW () {
let fetchRequest: NSFetchRequest<TextInputs> = TextInputs.fetchRequest()
do {
//go get the results
let searchResults18CW = try getContext().fetch(fetchRequest)
if indexPageSum == 18 {
for trans in searchResults18CW as [NSManagedObject] {
let result = trans.value(forKey: "cWeight")
if result != nil {
CWeight.text = result! as? String
}
}
}
}catch {
print("Error with request: \(error)")
}
}
然后保存。
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
if indexPageSum == 18 {
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 {
}
}
然后保存另一部分。
if indexPageSum == 18 {
let CWConvert = Double(CWeight.text!)
storeTranscription18CW(pageText: (CWConvert)!, textFileUrlString: "cWeight")
【问题讨论】:
标签: swift string core-data double