Saving an integer in core data

天大地大妈咪最大 提交于 2019-12-11 00:07:08

问题


Overview

I need to save several TextFields into CoreData, but only the first one (Seen as pickerView below) saves and prints correctly. The others do not save correctly, for instance, when I try to save the integer ones, I get an error saying that they cannot take a String, which makes sense. I just cannot find a way to fix the integer-string issue. The other error occurs when I attempted to cast everything as a string ( mainly because I won't need to do any arithmetic on it, so it doesn't matter ), and it just gives me a breaking point in the saveButton function.

What I would like to know

What I ultimately need is the ability to save all of these TextFields into CoreData so that I can later retrieve them. I appreciate the help in advance. Thank you!

NOTE

I am including the entire ( or most of ) the ViewController.swift file so that you can see how I am declaring things and then how they are being called. The code in question is located in the saveButton action at the bottom of the code block.

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")
    }


}

EDIT

Upon trying to save everything as just a string, since i only need to retrieve it, I run into this issue:

    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"))

I also get "(lldb)" in the debugger window.


回答1:


For things that don't need arithmetic, define them as strings in Core Data. For other numbers, it should work to do as you have with Int(YOM.text!).

However, I suggest that you create a managed object subclass for "UsedInfo" so that you can work directly with its properties instead of using setValue:forKey:. The benefit of a subclass is that it will show you data types explicitly.




回答2:


I'll just show you how to get int from string. Use it accordingly:

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

You can assign the text field to aString and convert it to Int like i showed you.




回答3:


Validate all textfields before trying to store,set the appropriate keyboard for each textfield and provide the valid character set for each textfield.

For Example:

YOM text field : Use Keyboard with only integers.

Valid character set are 0 to 9

And validation for min and max if applicable.

If any of the validation criteria fails ,throw an alert to input valid data.

I guess this solves your issue.



来源:https://stackoverflow.com/questions/37923636/saving-an-integer-in-core-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!