Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios don';如果文本字段为空,则无法保存_Ios_Swift_Swift3 - Fatal编程技术网

Ios don';如果文本字段为空,则无法保存

Ios don';如果文本字段为空,则无法保存,ios,swift,swift3,Ios,Swift,Swift3,我正在做一个大学项目,我遇到了一个我不知道如何解决的问题。当用户点击注册按钮时,他的信息将被保存,但如果他没有填写空格,他将收到一条错误消息。但问题是它会在CoreData中保存一个空文本字段。因此,我想“捕获”它,并在不将空文本字段保存到CoreData的情况下给它们一个错误 import UIKit import CoreData class ViewRegister: UIViewController { @IBOutlet weak var txtName: UITextFie

我正在做一个大学项目,我遇到了一个我不知道如何解决的问题。当用户点击注册按钮时,他的信息将被保存,但如果他没有填写空格,他将收到一条错误消息。但问题是它会在CoreData中保存一个空文本字段。因此,我想“捕获”它,并在不将空文本字段保存到CoreData的情况下给它们一个错误

import UIKit
import CoreData

class ViewRegister: UIViewController {
    @IBOutlet weak var txtName: UITextField!
    @IBOutlet weak var txtUsername: UITextField!
    @IBOutlet weak var txtPassword: UITextField!
    @IBOutlet weak var txtPhone: UITextField!
    @IBOutlet weak var txtAddress: UITextField!
    @IBOutlet weak var txtCity: UITextField!

    @IBAction func btnRegister(_ sender: Any) {

        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        let context = appDelegate.persistentContainer.viewContext

        let newContact = NSEntityDescription.insertNewObject(forEntityName: "Users", into: context)
        newContact.setValue(txtName.text, forKey: "name")
        newContact.setValue(txtUsername.text, forKey: "username")
        newContact.setValue(txtPassword.text, forKey: "password")
        newContact.setValue(txtPhone.text , forKey: "phone")
        newContact.setValue(txtAddress.text , forKey: "address")
        newContact.setValue(txtCity.text , forKey: "city")
        do{
            try context.save()
            print ("Saved")
        }
        catch{
            print ("Error")
        }

        if txtCity.text == "" || txtAddress.text == "" || txtPhone.text == "" || txtPassword.text == "" || txtUsername.text == "" || txtName.text == ""
        {
            // Create the alert controller
            let alert = UIAlertController(title: "Ops!!", message: "Please fill in the information", preferredStyle: .alert)

            // Create the actions
            let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
                UIAlertAction in
                NSLog("OK Pressed")
            }
            let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel) {
                UIAlertAction in
                NSLog("Cancel Pressed")
            }

            // Add the actions
            alert.addAction(okAction)
            alert.addAction(cancelAction)

            // Present the controller
            self.present(alert, animated: true, completion: nil)
        }
        else
        {
            txtName.text = ""
            txtUsername.text = ""
            txtPassword.text = ""
            txtPhone.text = ""
            txtAddress.text = ""
            txtCity.text = ""


            // Create the alert controller
            let alert = UIAlertController(title: "Successfully Registered!", message: "Thank you for registering in our application", preferredStyle: .alert)

            // Create the actions
            let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
                UIAlertAction in
                NSLog("OK Pressed")
            }
            let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel) {
                UIAlertAction in
                NSLog("Cancel Pressed")
            }

            // Add the actions
            alert.addAction(okAction)
            alert.addAction(cancelAction)

            // Present the controller
            self.present(alert, animated: true, completion: nil)
        }

    }


}

我只能重复@Lepidopteron的评论:为什么不调用联系人初始化并将调用保存在“else”代码部分

像这样

@IBAction func btnRegister(_ sender: Any) {        

    if txtCity.text == "" || txtAddress.text == "" || txtPhone.text == "" || txtPassword.text == "" || txtUsername.text == "" || txtName.text == ""
    {
        // Create the alert controller
        let alert = UIAlertController(title: "Ops!!", message: "Please fill in the information", preferredStyle: .alert)

        // Create the actions
        let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
            UIAlertAction in
            NSLog("OK Pressed")
        }
        let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel) {
            UIAlertAction in
            NSLog("Cancel Pressed")
        }

        // Add the actions
        alert.addAction(okAction)
        alert.addAction(cancelAction)

        // Present the controller
        self.present(alert, animated: true, completion: nil)
    }
    else
    {

    let appDelegate = UIApplication.shared.delegate as! AppDelegate
        let context = appDelegate.persistentContainer.viewContext

        let newContact = NSEntityDescription.insertNewObject(forEntityName: "Users", into: context)
        newContact.setValue(txtName.text, forKey: "name")
        newContact.setValue(txtUsername.text, forKey: "username")
        newContact.setValue(txtPassword.text, forKey: "password")
        newContact.setValue(txtPhone.text , forKey: "phone")
        newContact.setValue(txtAddress.text , forKey: "address")
        newContact.setValue(txtCity.text , forKey: "city")
        do{
            try context.save()
            print ("Saved")
        }
        catch{
             print ("Error")
        }


        txtName.text = ""
        txtUsername.text = ""
        txtPassword.text = ""
        txtPhone.text = ""
        txtAddress.text = ""
        txtCity.text = ""


        // Create the alert controller
        let alert = UIAlertController(title: "Successfully Registered!", message: "Thank you for registering in our application", preferredStyle: .alert)

        // Create the actions
        let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
            UIAlertAction in
            NSLog("OK Pressed")
        }
        let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel) {
            UIAlertAction in
            NSLog("Cancel Pressed")
        }

        // Add the actions
        alert.addAction(okAction)
        alert.addAction(cancelAction)

        // Present the controller
        self.present(alert, animated: true, completion: nil)
    }

}

为什么不调用联系人初始化并将调用保存在“else”代码部分。因为之前检查了if条件,所以这些值是否无效?您是说
context.save()
无条件的。如果您不想这样做,那么就不要这样做。@matt我是Xcode新手,因此请帮助在所有字段都有效之前不要启用“注册”按钮。例如,请参见
UITextFieldDelegate
协议。但您是新思维吗?如果你不想撞到墙上,就不要撞到墙上然后绕着墙走;在你撞到它之前先绕着它走一圈。您正在保存,然后检查是否应该保存。这就是问题所在。