2016-06-20 211 views
2

概述保存在覈心數據

我需要幾個的TextField保存到CoreData的整數,但是隻有第一個(見於如下pickerView)保存和正確地打印。其他人不能正確保存,例如,當我嘗試保存整數時,我得到一個錯誤,說他們不能接受一個字符串,這是有道理的。我只是找不到解決整數字符串問題的方法。另一個錯誤發生在我試圖將所有東西都轉換爲字符串時(主要是因爲我不需要對它進行任何算術運算,所以它沒有關係),它只是給了我一個saveButton函數的突破點。

我想知道

我最終需要的是所有這些TextField的保存到CoreData,這樣我可以取回這些數據的能力是什麼。我很感激提前的幫助。謝謝!

注意

我在內的整個(或大部分)的ViewController.swift文件,以便你可以看到我是如何聲明的東西,然後它們是如何被調用。有問題的代碼位於代碼塊底部的saveButton動作中。

CODE

@IBOutlet weak var locationOfMachine: UITextField! 
@IBOutlet weak var engineHours: UITextField! 
@IBOutlet weak var YOM: UITextField! 
@IBOutlet weak var serialNo: UITextField! 
@IBOutlet weak var modelName: UITextField! 
@IBOutlet weak var pickerTextField: UITextField! 
var pickOption = ["Wirtgen","Kleeman","Hamm","Vögele"] 
    override func viewDidLoad() { 
    super.viewDidLoad() 
    var pickerView = UIPickerView() 
    pickerView.delegate = self 


    pickerTextField.inputView = pickerView 
    // Do any additional setup after loading the view, typically from a nib. 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int { 
    return 1 
} 




@IBAction func saveButton(sender: AnyObject) 
{ 
    var appDel: AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate) 
    var context:NSManagedObjectContext = appDel.managedObjectContext 

    var entity1 = NSEntityDescription.insertNewObjectForEntityForName("UsedInfo", inManagedObjectContext:context) as NSManagedObject 
    entity1.setValue(pickerTextField.text, forKey: "product") 
    entity1.setValue(modelName.text, forKey:"modelName") 
    entity1.setValue(serialNo.text, forKey:"serialNo") 
    entity1.setValue(Int(YOM.text!), forKey:"yom") 
    entity1.setValue(engineHours.text, forKey:"engineHours") 
    entity1.setValue(locationOfMachine.text, forKey:"location") 
    print(entity1.valueForKey("product")) 
    print(entity1.valueForKey("modelName")) 
    print(entity1.valueForKey("serialNo")) 
    print(entity1.valueForKey("yom")) 
    print(entity1.valueForKey("engineHours")) 


    do { 
     try context.save() 
    } 
    catch { 
     print("error") 
    } 


} 

編輯

在嘗試一切保存爲只是一個字符串,因爲我只需要找回它,我碰到這個問題:

entity1.setValue(pickerTextField.text, forKey: "product") 
    entity1.setValue(modelName.text, forKey:"modelName") 
    entity1.setValue(serialNo.text, forKey:"serialNo") <-Thread1:Breakpoint1.1 
    entity1.setValue(YOM.text, forKey:"yom") 
    entity1.setValue(engineHours.text, forKey:"engineHours") 
    entity1.setValue(locationOfMachine.text, forKey:"location") 
    print(entity1.valueForKey("product")) 
    print(entity1.valueForKey("modelName")) 
    print(entity1.valueForKey("serialNo")) 
    print(entity1.valueForKey("yom")) 
    print(entity1.valueForKey("engineHours")) 

我也在調試器窗口中獲得「(lldb)」。

回答

0

對於不需要算術的東西,將它們定義爲核心數據中的字符串。對於其他數字,它應該像Int(YOM.text!)一樣工作。

但是,我建議您爲「UsedInfo」創建一個託管對象子類,以便您可以直接使用其屬性而不是使用setValue:forKey:。子類的好處是它會顯式地顯示數據類型。

+0

謝謝!同樣,這裏有很大的noob,但是對象子類究竟是什麼? – gavsta707

+0

當您使用核心數據模型時,Xcode的編輯器菜單有一個「Create NSManagedObject Subclass ...」選項。它可以讓你創建符合你的設計的特定類,而不是總是處理泛型的'NSManagedObject'類型。 –

+0

謝謝!我會研究這個 – gavsta707

0

我會告訴你如何從字符串中獲取int。因此使用它:

var aString = "0000" // var aString = textField.text! 
var numFromString = Int(aString) 

您可以將文本字段分配給ASTRING並將其轉換爲Int像我向您展示。

+0

謝謝!現在我到了一半!我很欣賞對noob問題的快速響應。 – gavsta707

+0

檢查我編輯的答案,直接將textField的文本分配給一個變量並將其用於轉換爲Int。快樂的編碼! :) – Dershowitz123

0

嘗試存儲之前驗證所有文本字段,爲每個文本字段設置適當的鍵盤併爲每個文本字段提供有效的字符集。

例如:

YOM文本字段:使用鍵盤只有整數。

有效的字符集是0到9

和驗證min和max(如果適用)。

如果有任何驗證條件失敗,請發出警報以輸入有效數據。

我想這解決了你的問題。