菜鸟教程小白 发表于 2022-12-11 16:46:04

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


                                            <p><p><strong>概述</strong></p>

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

<p><strong>我想知道什么</strong></p>

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

<p><strong>注意</strong></p>

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

<p><strong>代码</strong></p>

<pre><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 = [&#34;Wirtgen&#34;,&#34;Kleeman&#34;,&#34;Hamm&#34;,&#34;Vögele&#34;]
   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) -&gt; 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(&#34;UsedInfo&#34;, inManagedObjectContext:context) as NSManagedObject
    entity1.setValue(pickerTextField.text, forKey: &#34;product&#34;)
    entity1.setValue(modelName.text, forKey:&#34;modelName&#34;)
    entity1.setValue(serialNo.text, forKey:&#34;serialNo&#34;)
    entity1.setValue(Int(YOM.text!), forKey:&#34;yom&#34;)
    entity1.setValue(engineHours.text, forKey:&#34;engineHours&#34;)
    entity1.setValue(locationOfMachine.text, forKey:&#34;location&#34;)
    print(entity1.valueForKey(&#34;product&#34;))
    print(entity1.valueForKey(&#34;modelName&#34;))
    print(entity1.valueForKey(&#34;serialNo&#34;))
    print(entity1.valueForKey(&#34;yom&#34;))
    print(entity1.valueForKey(&#34;engineHours&#34;))


    do {
      try context.save()
    }
    catch {
      print(&#34;error&#34;)
    }


}
</code></pre>

<p><strong>编辑</strong></p>

<p>在尝试将所有内容保存为字符串时,因为我只需要检索它,我遇到了这个问题:</p>

<pre><code>    entity1.setValue(pickerTextField.text, forKey: &#34;product&#34;)
    entity1.setValue(modelName.text, forKey:&#34;modelName&#34;)
    entity1.setValue(serialNo.text, forKey:&#34;serialNo&#34;) &lt;-Thread1:Breakpoint1.1
    entity1.setValue(YOM.text, forKey:&#34;yom&#34;)
    entity1.setValue(engineHours.text, forKey:&#34;engineHours&#34;)
    entity1.setValue(locationOfMachine.text, forKey:&#34;location&#34;)
    print(entity1.valueForKey(&#34;product&#34;))
    print(entity1.valueForKey(&#34;modelName&#34;))
    print(entity1.valueForKey(&#34;serialNo&#34;))
    print(entity1.valueForKey(&#34;yom&#34;))
    print(entity1.valueForKey(&#34;engineHours&#34;))
</code></pre>

<p>我还在调试器窗口中得到“(lldb)”。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我将向您展示如何从字符串中获取 int。相应地使用它:</p>

<pre><code>var aString = &#34;0000&#34; // var aString = textField.text!
var numFromString = Int(aString)
</code></pre>

<p>您可以将文本字段分配给 aString 并将其转换为 <code>Int</code>,就像我向您展示的那样。 </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 在核心数据中保存一个整数,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/37923636/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/37923636/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 在核心数据中保存一个整数