2017-04-20 166 views
-1

所以我创建了一个数组或所有来自我的数据库的信息,并将其设置为包含单个较小字符串的大字符串。我想创建一个基本验证它的语句,并说如果字符串位于字符串的字符串中,弹出警告。我似乎无法创造出这样的正确声明。搜索字符串中的字符串

下面

是我的代码创建大型字符串,并命名为USERINFO

// 
// ViewController.swift 
// WagerMe 
// 
// Created by Christopher calvert on 3/1/17. 
// Copyright © 2017 Christopher calvert. All rights reserved. 
// CODED BY CHRIS CALVERT 

import UIKit 

class ViewController: UIViewController { 


    var userInfo = [[String]]() 



    @IBOutlet weak var fullname: UITextField! 
    @IBOutlet weak var userAge: UITextField! 
    @IBOutlet weak var userUsername: UITextField! 
    @IBOutlet weak var userEmail: UITextField! 
    @IBOutlet weak var userPassword: UITextField! 

    @IBAction func register(_ sender: Any) { 
     let stuff = userInfo 

     if(fullname.text!.isEmpty || userAge.text!.isEmpty || userUsername.text!.isEmpty || userEmail.text!.isEmpty || userPassword.text!.isEmpty) { 

      let myAlert = UIAlertController(title: "Alert", message: "All fields are required to register.", preferredStyle: UIAlertControllerStyle.alert); 
      let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler:nil) 
      myAlert.addAction(okAction); 
      self.present(myAlert, animated: true, completion: nil) 

      return; 
     } 

     func validateAlpha(field: [String]) -> Bool { 
      let alphanumericChars = NSCharacterSet(charactersIn: "[email protected]#$%^&*()-_=+1234567890.,") 

      var result = false 
      for x in field { 
       if (x.rangeOfCharacter(from: alphanumericChars as CharacterSet) != nil) { 
        result = true 
       } 
       else { 
        result = false 
       } 
      } 
      return result 
     } 

     func validateAge(field: [String]) -> Bool { 
      let alphanumericChars = NSCharacterSet(charactersIn: "[email protected]#$%^&*()-_=+.,abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") 

      var result = false 
      for x in field { 
       if (x.rangeOfCharacter(from: alphanumericChars as CharacterSet) != nil) { 
        result = true 
       } 
       else { 
        result = false 
       } 
      } 
      return result 
     } 

     func validateEmail(field: [String]) -> Bool { 
      let alphanumericChars = NSCharacterSet(charactersIn: "!#$%^&*()-_=+,") 

      var result = false 
      for x in field { 
       if (x.rangeOfCharacter(from: alphanumericChars as CharacterSet) != nil) { 
        result = true 
       } 
       else { 
        result = false 
       } 
      } 
      return result 
     } 

     func validate(field: [String]) -> Bool { 
      let alphanumericChars = NSCharacterSet(charactersIn: "[email protected]#$%^&*()-_=+") 

      var result = false 

      for x in field { 
       if (x.rangeOfCharacter(from: alphanumericChars as CharacterSet) != nil) { 
        result = true 
       } 
       else { 
        result = false 
       } 
      } 
      return result 
     } 



     if validateAlpha(field: [fullname.text!]) { 
      let validateName = UIAlertController(title: "Alert", message: "You have entered invalid characters in the 'Name' field", preferredStyle: UIAlertControllerStyle.alert) 

      validateName.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil)) 

      self.present(validateName, animated: true, completion: nil) 

     } else if validateAge(field: [userAge.text!]) { 
      let validateAge = UIAlertController(title: "Alert", message: "You have entered non-numbers in the 'Age' field", preferredStyle: UIAlertControllerStyle.alert) 

      validateAge.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil)) 

      self.present(validateAge, animated: true, completion: nil) 



     } else if validate(field: [userUsername.text!]) { 
      let validateUsername = UIAlertController(title: "Alert", message: "You have entered invalid characters in the 'Username' field", preferredStyle: UIAlertControllerStyle.alert) 

      validateUsername.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil)) 

      self.present(validateUsername, animated: true, completion: nil) 
     } else if validateEmail(field: [userEmail.text!]) { 
      let validateEmail = UIAlertController(title: "Alert", message: "You have entered invalid characters in the 'Email' field", preferredStyle: UIAlertControllerStyle.alert) 

      validateEmail.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil)) 

      self.present(validateEmail, animated: true, completion: nil) 
     } else if validate(field: [userPassword.text!]) { 
      let validatePassword = UIAlertController(title: "Alert", message: "You have entered invalid characters in your password.", preferredStyle: UIAlertControllerStyle.alert) 

      validatePassword.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil)) 

      self.present(validatePassword, animated: true, completion: nil) 
     } else if (fullname.text?.characters.count)! > 31 { 
      let nameTooLong = UIAlertController(title: "Alert", message: "Your name must be less than 30 characters.", preferredStyle: UIAlertControllerStyle.alert) 

      nameTooLong.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil)) 

      self.present(nameTooLong, animated: true, completion: nil) 
     } else if (fullname.text?.characters.count)! < 5 { 
      let nameTooShort = UIAlertController(title: "Alert", message: "Your name must be greater than 4 characters.", preferredStyle: UIAlertControllerStyle.alert) 

      nameTooShort.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil)) 

      self.present(nameTooShort, animated: true, completion: nil) 
     } else if (userPassword.text?.characters.count)! < 5 { 
      let passwordTooShort = UIAlertController(title: "Alert", message: "Your password must be greater than 4 characters.", preferredStyle: UIAlertControllerStyle.alert) 

      passwordTooShort.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil)) 

      self.present(passwordTooShort, animated: true, completion: nil) 
     } else if (userPassword.text?.characters.count)! > 19 { 
      let passwordTooLong = UIAlertController(title: "Alert", message: "Your password must be less than 18 characters.", preferredStyle: UIAlertControllerStyle.alert) 

      passwordTooLong.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil)) 

      self.present(passwordTooLong, animated: true, completion: nil) 
     } else if (userAge.text?.characters.count)! < 2 { 
      let passwordTooLong = UIAlertController(title: "Alert", message: "Sorry! You're not old enough to register for WagerMe.", preferredStyle: UIAlertControllerStyle.alert) 

      passwordTooLong.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil)) 

      self.present(passwordTooLong, animated: true, completion: nil) 
     } else if (userUsername.text?.characters.count)! > 21 { 
      let usernameTooLong = UIAlertController(title: "Alert", message: "Your username must be less than 20 characters.", preferredStyle: UIAlertControllerStyle.alert) 

      usernameTooLong.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil)) 

      self.present(usernameTooLong, animated: true, completion: nil) 
     } else if (userUsername.text?.characters.count)! < 5 { 
      let usernameTooShort = UIAlertController(title: "Alert", message: "Your username must be greater than 4 characters.", preferredStyle: UIAlertControllerStyle.alert) 

      usernameTooShort.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil)) 

      self.present(usernameTooShort, animated: true, completion: nil) 

     } else { 




      let request = NSMutableURLRequest(url: NSURL(string: "http://cgi.soic.indiana.edu/~team10/store.php")! as URL) 
      request.httpMethod = "POST" 

      let postString = "a=\(fullname.text!)&b=\(userAge.text!)&c=\(userUsername.text!)&d=\(userEmail.text!)&e=\(userPassword.text!)" 

      request.httpBody = postString.data(using: String.Encoding.utf8) 

      let task = URLSession.shared.dataTask(with: request as URLRequest) { 
       data, response, error in 

       if error != nil { 
        print("error=\(String(describing: error))") 
        return 
       } 

       print("response = \(String(describing: response))") 

       let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue) 
       print("responseString = \(String(describing: responseString))") 
      } 

      task.resume() 
     } 
     //Alert for successful registration 

     let alertController = UIAlertController(title: "User", message: "User successfully registered!", preferredStyle: UIAlertControllerStyle.alert) 
     alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)) 

     self.present(alertController, animated: true, completion: nil) 

     fullname.text = "" 
     userAge.text = "" 
     userEmail.text = "" 
     userUsername.text = "" 
     userPassword.text = "" 


    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     let url = URL(string:"http://cgi.soic.indiana.edu/~team10/pullUsers.php") 
     do { 
      let userdata = try Data(contentsOf: url!) 

      let users = try JSONSerialization.jsonObject(with: userdata, options: JSONSerialization.ReadingOptions.allowFragments) as! [String:AnyObject] 
      if let jsonarray = users["users"] as? NSArray{ 
       for index in 0...jsonarray.count-1{ 
        var array = [String]() 
        let object = jsonarray[index] as! [String : AnyObject] 
        let username = object["username"] as! String 
        let email = object["email"] as! String 


        array.append(username) 
        array.append(email) 
        userInfo.append(array) 
       } 


      } 
     } 
     catch{ 
     } 
     print(userInfo) 

     //Looks for single or multiple taps. 
     let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(LoginViewController.dismissKeyboard)) 

     //Uncomment the line below if you want the tap not not interfere and cancel other interactions. 
     //tap.cancelsTouchesInView = false 

     view.addGestureRecognizer(tap) 
     // 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. 
    } 
    //Calls this function when the tap is recognized. 
    func dismissKeyboard() { 
     //Causes the view (or one of its embedded text fields) to resign the first responder status. 
     view.endEditing(true) 
    } 



} 

我的创作数组并将其设置为一串一串的viewDidLoad中

回答

0

启动代码的调查后我建议您创建代表用户的struct

这是因为,从收到的响应:

http://cgi.soic.indiana.edu/~team10/pullUsers.php 

即:

{ “用户”:[{ “用户名”: “sspoulos”, “电子邮件”: “[email protected]”},{ “用户名”: “ctcalver”, “电子邮件”: “[email protected]”},{ “用户名”: “zjhabib”, “电子邮件”:“[email protected] “},{” 用户名 “:” aukmoore “ ”电子邮件“: ”[email protected]“},{ ”用户名“: ”测试仪“, ”电子邮件“: ”测试仪“},{ ”用户名“:” 奥斯汀”, “电子邮件”: “电子邮件”},{ “用户名”: “赞恩”, “电子邮件”: “赞恩”},{ “用户名”: “kbryant”, “电子邮件”: “[email protected]”} { “用户名”: “rartest”,“鄂麦L “:” [email protected] “},{” 用户名 “:” assman “ ”电子邮件“: ”[email protected]“},{ ”用户名“: ”tromo“, ”电子邮件“:” tromo @ indiana.edu “},{” 用户名 “:” emanning”, “电子邮件”: “[email protected]”},{ “用户名”: “steventest”, “电子邮件”: “steventest”},{ “用户名” : “hsimpson”, “电子邮件”: “[email protected]”},{ “用户名”: “pgriffin”, “电子邮件”: “[email protected]”},{ “用户名”: “abelinc”,”电子邮件 “:” [email protected] “},{” 用户名 “:” akmoore”, “电子邮件”: “[email protected]”}]}

实施例:

struct User { 
    let username: String 
    let email: String 
} 

在此之后,您将创建User数组而不是巨大的字符串。这将使您有机会验证,以您想要的方式过滤数据,并且语法清晰。

验证的例子,如果用户在数组:

let users = [User(name: "sspoulos", email: "[email protected]"),User(name: "ctcalver", email: "[email protected]"),User(name: "zjhabib", email: "[email protected]"),User(name: "aukmoore", email: "[email protected]")] 


let result = users.contains{$0.name == "sspoulos"} 

这样,你要知道,如果用户在数组中提出,如果没有自动显示警告。