简体   繁体   English

保存由textFieldDidEndEditing更改的textfield值是否不会保存在核心数据中?

[英]Saving textfield value altered by textFieldDidEndEditing won't save in core data?

I am using the following to update a amountVAT based on textfield totalAmountInc 我正在使用以下内容基于文本字段totalAmountInc更新amountVAT

func textFieldDidEndEditing(textField: UITextField) {
    if totalAmountInc != nil {
        let tmpVAT = Double(totalAmountInc.text!)
        let valueVAT = getVATValue(tmpVAT!)

        //textfield2
        amountVAT.text = String(valueVAT)
    }
}

But now the value won't save (using Core Data)? 但是现在(使用核心数据)值将无法保存?

When i directly enter a amountVAT value it will be saved. 当我直接输入一个amountVAT值时,它将被保存。

Saving process looks like 保存过程看起来像

func newItem(){

    let context = self.context
    let ent = NSEntityDescription.entityForName("List", inManagedObjectContext: context)


    let nItem = List(entity: ent!, insertIntoManagedObjectContext: context)
    let numberFormatter = NSNumberFormatter()

    nItem.amountVAT = numberFormatter.numberFromString(amountVAT.text!)
    .....more values
    nItem.invoiceImage = UIImagePNGRepresentation(imageHolder.image!)

    do {
     try context.save()
    } catch {
        print(error)
    }
}
func saveValues(sender: AnyObject) {

        if nItem != nil {
            editItem()
        } else {
            newItem()
        }

        dismissVC()
    }

You need to examine what the variable you are trying to save to Core Data contains. 您需要检查要保存到Core Data中的变量。 Set a breakpoint and see where you made the mistake. 设置一个断点,看看您在哪里出错。

In your code sample, there are a number of issues. 在您的代码示例中,存在许多问题。 For example, in textFieldDidEndEditing the text field in question is passed as the textField variable - no reason to refer to your instance variable and check for nil . 例如,在textFieldDidEndEditing中,有问题的文本字段作为textField变量传递-没有理由引用您的实例变量并检查nil (Perhaps you intended to check if the text property is nil ? It is preferable to initialize it to "" and not bother with this check. You are not explaining what getVATValue does, presumably just calculating a percentage. A function seems overkill for something trivial as that (and it makes your code less readable). (也许您打算检查text属性是否为nil ?最好将其初始化为""而不用为此检查打扰。您没有解释getVATValue作用,大概只是计算一个百分比。对于一些琐碎的事情,函数似乎过大了。那样(这会使您的代码可读性降低)。

Check what the result of Double(textField.text!) is. 检查Double(textField.text!)的结果是什么。 It is likely that you find some problem with the initialization of the Double value. 您可能会发现Double值的初始化存在问题。

Also, it does not make sense to put the value into the VAT text field, and then at save time re-convert it into a number to save in Core Data. 同样,将值放入VAT文本字段中然后在保存时将其重新转换为数字以保存在Core Data中也没有意义。 You are using an UI element to hold value (in an inconvenient format). 您正在使用UI元素保存值(格式不方便)。 Instead, you should be using variables of type Double to hold the values you need. 相反,您应该使用Double类型的变量来保存所需的值。

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

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