我想知道是否有更简洁的方法来处理这种注册验证,尤其是最后一节有三个if块。为了完整性,我要说每个警报都有不同的消息要显示。
func fieldsAreValid() -> Bool {
guard let email = (view.viewWithTag(1) as? UITextField)?.text,
let password = (view.viewWithTag(2) as? UITextField)?.text else {
// show an alert
return false
}
if !email.isEmail {
// show an alert
return false
}
if !password.isAStrongPassword {
// show an alert
return false
}
if isASignUp() {
if let verification = (view.viewWithTag(3) as? UITextField)?.text {
if verification != password {
// show an alert
return false
}
} else {
// show an alert
return false
}
}
return true
}
答案 0 :(得分:0)
这是一个建议:
> if(isset($_FILES["fileToUpload"]["name"]) && $_FILES["fileToUpload"]["name"] != "")
> {
> $target_dir = "/pics/";
> $target_file = 'Image_'.basename($_FILES["fileToUpload"]["name"]);
>
> $uploadOk = 1;
> $imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);
>
> // Check if image file is a actual image or fake image
> $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
>
> if ($check !== false) {
> $msg['status'] = '1';
> $msg['message'] = "File is an image - " . $check["mime"] . ".";
> $uploadOk = 1;
> } else {
> $msg['status'] = '0';
> $msg['message'] = "File is not an image.";
> $uploadOk = 0;
> }
>
> // Check if file already exists
> if (file_exists($target_file)) {
> $msg['status'] = '0';
> $msg['message'] = "Sorry, file already exists.";
> $uploadOk = 0;
> }
> // Check file size
> if ($_FILES["fileToUpload"]["size"] > 5000000) {
> $msg['status'] = '0';
> $msg['message'] = "Sorry, your file is too large.";
> $uploadOk = 0;
> }
> // Allow certain file formats
> if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif") {
> $msg['status'] = '0';
> $msg['message'] = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
> $uploadOk = 0;
> }
>
> // Check if $uploadOk is set to 0 by an error
> if ($uploadOk == 0) {
> $msg['uploadstatus'] = "Sorry, your file was not uploaded.";
> // if everything is ok, try to upload file
> } else {
> $target_dir = "/pics/";
> if (!file_exists($target_dir)) {
> try {
> mkdir($target_dir);
> } catch (Exception $ex) {
> die("error");
> }
> }
> if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],
> $target_dir.$target_file)) {
> $data['imageTitle'] = basename($_FILES["fileToUpload"]["name"]);
> $data['imageLink'] = $target_file;
> $data['memberId'] = 5;
> $msg['id'] = 5;
> $msg['name'] = basename($_FILES["fileToUpload"]["name"]);
> $msg['size'] = ($_FILES["fileToUpload"]["size"] / 1024) . "kb";
> $msg['status'] = '1';
> $msg['message'] = "The file " . basename($_FILES["fileToUpload"]["name"]) . " has been uploaded.";
> $msg['uploadstatus'] = "file uploaded";
> $msg['imagepath'] = $target_dir;
>
> } else {
> $msg['status'] = '0';
> $msg['message'] = "Sorry, there was an error uploading your file.";
> $msg['uploadstatus'] = "Sorry, your file was not uploaded.";
> }
>
> }
> }
我认为最好使用func fieldsAreValid() -> Bool {
let emailField = view.viewWithTag(0) as! UITextField
let passwordField = view.viewWithTag(1) as! UITextField
let verificationField = view.viewWithTag(2) as? UITextField
guard let email = emailField.text,
let password = passwordField.text else {
return false
}
let isEmail = email.isEmail
let isStrongPassword = password.isAStrongPassword
let isLoginOrValidSignup = !isASignup() || verificationField?.text == password
return isEmail && isStrongPassword && isLoginOrValidSignup
}
func validateForm() -> Bool {
let valid = fieldsAreValid()
if !valid {
// show alert
}
return valid
}
将视图转换为as!
,因为这应该始终有效。如果说,带有标记0的视图不是UITextField
,您可能希望在开发过程中找到它,因此可以在那里崩溃。
答案 1 :(得分:0)
守卫声明
这是一个非常好的解释:http://ericcerney.com/swift-guard-statement/
在你的情况下,我会写:
func fieldsAreValid() -> Bool {
guard let email = (view.viewWithTag(0) as? UITextField)?.text,
let password = (view.viewWithTag(1) as? UITextField)?.text else {
// show an alert
return false
}
guard email.isEmail else {
// show an alert
return false
}
guard password.isAStrongPassword else {
// show an alert
return false
}
if isASignUp() {
guard let verification = (view.viewWithTag(2) as? UITextField)?.text else {
// show an alert
return false
}
guard verification == password else {
// show an alert
return false
}
//here your code if isASignUp is true
}
//here your code if it's a login ;)
return true
}