OStack程序员社区-中国程序员成长平台

标题: ios - 在核心数据中保存一个整数 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 16:46
标题: ios - 在核心数据中保存一个整数

概述

我需要将几个 TextField 保存到 CoreData 中,但只有第一个(如下面的 pickerView 所示)可以正确保存和打印。其他的没有正确保存,例如,当我尝试保存整数时,我收到一个错误,说他们不能接受字符串,这是有道理的。我只是找不到解决整数字符串问题的方法。当我尝试将所有内容都转换为字符串时会发生另一个错误(主要是因为我不需要对其进行任何算术运算,所以没关系),它只是在 saveButton 函数中给了我一个断点。

我想知道什么

我最终需要的是能够将所有这些 TextField 保存到 CoreData 中,以便我以后可以检索它们。我提前感谢您的帮助。谢谢!

注意

我包含了整个(或大部分) ViewController.swift 文件,以便您可以看到我是如何声明事物以及如何调用它们的。有问题的代码位于代码块底部的 saveButton 操作中。

代码

@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)”。



Best Answer-推荐答案


我将向您展示如何从字符串中获取 int。相应地使用它:

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

您可以将文本字段分配给 aString 并将其转换为 Int,就像我向您展示的那样。

关于ios - 在核心数据中保存一个整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37923636/






欢迎光临 OStack程序员社区-中国程序员成长平台 (http://ostack.cn/) Powered by Discuz! X3.4