简体   繁体   English

在核心数据中保存一个整数

[英]Saving an integer in core data

Overview 概观

I need to save several TextFields into CoreData, but only the first one (Seen as pickerView below) saves and prints correctly. 我需要将几个TextField保存到CoreData中,但是只有第一个(正确显示为pickerView)可以保存并正确打印。 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. 其他的不能正确保存,例如,当我尝试保存整数时,我收到一条错误消息,指出它们不能采用String,这是有道理的。 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. 当我尝试将所有内容都转换为字符串时,会发生另一个错误(主要是因为我不需要对它进行任何算术运算,所以没关系),这只是给我saveButton函数的一个断点。

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. 我最终需要的是能够将所有这些TextField保存到CoreData中的功能,以便以后可以检索它们。 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. 我包括整个(或大部分)ViewController.swift文件,以便您可以看到我如何声明事物以及如何调用它们。 The code in question is located in the saveButton action at the bottom of the code block. 有问题的代码位于代码块底部的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")
    }


}

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. 我也在调试器窗口中获得“(lldb)”。

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. 您可以将文本字段分配给aString并将其转换为Int就像我向您展示的那样。

For things that don't need arithmetic, define them as strings in Core Data. 对于不需要算术的事情,请在Core Data中将它们定义为字符串。 For other numbers, it should work to do as you have with Int(YOM.text!) . 对于其他数字,它应该可以像处理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: . 但是,我建议您为“ UsedInfo” 创建一个托管对象子类 ,以便可以直接使用其属性,而不是使用setValue:forKey: The benefit of a subclass is that it will show you data types explicitly. 子类的好处是它将显式显示数据类型。

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. YOM文本字段:仅对整数使用键盘。

Valid character set are 0 to 9 有效字符集为0到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. 我想这可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM