私は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 {
}
}
とretriveします。
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)")
}
}
私は別の名前を試してみましたが、最後の1アウト私の瞬間は、最初は、実際の値を示した場合、それが唯一の実数として最後のものを示し同じを取得しています。
同じテキストフィールドに複数の値のtWeightを表示しようとしていますか?これはテーブルビューですか? – MwcsMac
こんにちは、これは2つの異なるテキストフィールドですが、コアデータの文字列として設定し、2つの代わりに任意の値を設定すると動作しますが、計算に付属する数値を変換するためにDoubleにする必要があります。 –
なぜ同じ保存関数に両方の数値を持たないのですか? 2つの異なる関数に保存すると、保存ごとに新しい行が作成されます。だから、現在の体重3を保存すると、Targetは無しとして保存されます。あなたが目標を保存するときも同じことが起こります。 – MwcsMac