我在尝试创建检查用户输入和存储数据的函数时遇到上述错误。在我到达此函数 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()'
最佳答案
当你有一个嵌套函数时,你必须在调用它之前声明它。在这种情况下,将“displayAlertMessage(userMessage:String)”函数移到“//Check for empty fields”注释上方,然后它应该可以编译。
关于ios - Xcode 7 + swift 。 "Use of local variable ' _' before its declaration"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38598019/