Xcode 7 + swift。 "使用局部变量' _'在宣布之前#34;错误

时间:2016-07-26 19:07:34

标签: ios swift

尝试创建检查用户输入和存储数据的函数时出现上述错误。我的项目构建正常,直到我到达此函数RegisterButtonTapped()。有没有人有一些结构或语法更改可以摆脱这个错误?

   @IBAction func RegisterButtonTapped(sender: AnyObject) {

    let userEmail = userEmailTextField.text;
    let userPassword = userEmailTextField.text;
    let userRepeatPassword = userRepeatPasswordTextField.text;


    // Check for empty fields
    if(userEmail!.isEmpty || userPassword!.isEmpty || userRepeatPassword!.isEmpty){

        displayAlertMessage("All fields are required");

        return;
    }

    // Check if passwords match
    if(userPassword != userRepeatPassword){

        displayAlertMessage("Passwords do not match");

        return;
    }

    // Store Data
    NSUserDefaults.standardUserDefaults().setObject(userEmail, forKey: "userEmail");
    NSUserDefaults.standardUserDefaults().setObject(userEmail, forKey: "userPassword");
    NSUserDefaults.standardUserDefaults().synchronize();

    // Display Alert message with confirmation
    var myAlert = UIAlertController(title:"Alert",message:"Registration is successful, thank you.",preferredStyle: UIAlertControllerStyle.Alert);

    func displayAlertMessage(userMessage:String){

        var myAlert = UIAlertController(title:"Alert",message: userMessage, preferredStyle: UIAlertControllerStyle.Alert);

        let okAction = UIAlertAction(title:"ok",style: UIAlertActionStyle.Default, handler:nil);

        myAlert.addAction(okAction);
        self.presentViewController(myAlert, animated: true, completion: nil);
    } // END OF FUNCTION 'displayAlertMessage()'



} // END of FUNCTION 'RegisterButtonTapped()'

3 个答案:

答案 0 :(得分:0)

在调用它之后声明了displayAlertMessage,如果你想将它保存为嵌套函数,请将它的声明移到RegisterButtonTapped()的顶部附近,否则将它移出RegisterButtonTapped()。

除此之外,你有两个名为myAlert的变量,第一个是无用的,你将userEmail保存为电子邮件和密码,也不需要调用synchronize()。

答案 1 :(得分:0)

有一些细节可以让它正常运行。调用后displayAlertMessage在寄存器函数内声明,就像我们得到警告一样。当您获得成功时,必须使用成功消息调用该函数,而不是在警报函数内声明var myAlert。最后的细节:获取UITextFields值时,您获得了电子邮件输入字段并将其设置为密码值,因此验证将是错误的。

这里有一个很好的代码示例:

class ViewController: UIViewController {

    @IBOutlet var userEmailTextField:UITextField!
    @IBOutlet var userPassTextField:UITextField!
    @IBOutlet var userRepeatPasswordTextField:UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        // 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.
    }

    @IBAction func RegisterButtonTapped(sender:UIButton) {
        let userEmail = userEmailTextField.text;
        let userPassword = userPassTextField.text;
        let userRepeatPassword = userRepeatPasswordTextField.text;


        // Check for empty fields
        if(userEmail!.isEmpty || userPassword!.isEmpty || userRepeatPassword!.isEmpty){

            displayAlertMessage("All fields are required");

            return;
        }

        // Check if passwords match
        if(userPassword != userRepeatPassword){

            displayAlertMessage("Passwords do not match");

            return;
        }

        // Store Data
        NSUserDefaults.standardUserDefaults().setObject(userEmail, forKey: "userEmail");
        NSUserDefaults.standardUserDefaults().setObject(userEmail, forKey: "userPassword");
        NSUserDefaults.standardUserDefaults().synchronize();

        displayAlertMessage("Registration is successful, thank you.")
    }

    // Display Alert message with confirmation
    func displayAlertMessage(userMessage:String){

        let myAlert = UIAlertController(title:"Alert",message: userMessage, preferredStyle: UIAlertControllerStyle.Alert);

        let okAction = UIAlertAction(title:"ok",style: UIAlertActionStyle.Default, handler:nil);

        myAlert.addAction(okAction);
        self.presentViewController(myAlert, animated: true, completion: nil);
    }

}

答案 2 :(得分:0)

当你有一个嵌套函数时,你必须在之前声明它你可以调用它。在这种情况下,将“displayAlertMessage(userMessage:String)”函数移到“//检查空字段”注释上方,然后它应该编译。