2016-07-26 80 views
2

嘗試創建函數來檢查用戶輸入和存儲數據時,出現上述錯誤。我的項目建立良好,直到我達到這個功能RegisterButtonTapped()。有沒有人有一些結構或語法的變化,可以擺脫這個錯誤?Xcode 7 + swift。 「在聲明之前使用局部變量'_'錯誤

@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()' 
+0

附註 - 我已經嘗試清洗+重建項目,並重新啓動Xcode。 –

+0

你已經在RegisterButtonTapped裏面定義了'displayAlertMessage' ...並且使用了大小寫函數名稱。 – triandicAnt

+1

@ triple.s Swift嵌套函數 – Gruntcakes

回答

0

您已經聲明displayAlertMessage您呼叫後,將它的聲明附近RegisterButtonTapped()如果你想保持它作爲一個嵌套函數的頂部,否則將其移出RegisterButtonTapped的()。

除此之外,您有兩個變量,都稱爲myAlert,第一個變量沒用,並且您將userEmail保存爲電子郵件和密碼,並且不需要調用synchronize()。

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); 
    } 

} 
+0

「displayAlertMessage在註冊函數內聲明,就像我們得到的警告」。情況並非如此,Swift允許嵌套函數。 – Gruntcakes

+0

忘了說這個聲明是在調用之後。進行了更改。 –

+0

您的改進工作完美。代碼現在按預期運行。 –

0

當你有一個嵌套函數,你必須聲明它之前,你可以調用它。在這種情況下,將「displayAlertMessage(userMessage:String)」函數移動到「//檢查空字段」註釋上方,然後它應該進行編譯。

相關問題