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

标签 ios xcode swift core-data textfield

概览

我需要将几个 TextFields 保存到 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)”。

最佳答案

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

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

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

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

相关文章:

ios - MFMailComposeViewController 强制图像附件的实际大小

ios - 使用 SQLite3 隐式转换为 Objective-C 指针

ios - 如何将自定义 map 图钉添加到 Apple Watch 上的 map View ?

ios - 将日期插入到 xcode 中的 sqlite3 数据库中

ios - Objective-C 索引数组

xcode - Mogenerator 错误 : skipping entity MyObjectName (NSManagedObject) because it doesn't use a custom subclass.

ios - 如何将数据从 View Controller 传递到选项卡栏 Controller 的第一个选项卡

objective-c - 对象-c/iOS :About use NSUserDefaults set serial numbers/password

swift - 无法将项目附加到 CollectionView

iOS:使用 Word Wrap 以编程方式创建标签