Dailen Spencer Dailen Spencer - 1 month ago 6
Swift Question

Swift: Convert UITextField input to string and split string at certain character

I am trying to convert the text inside a UITextField into a string so that I can use the method "componentsSeperatedByString(".") on the string in order to turn it into an array. But I keep receiving an error that UITextField does not have member "componentsSeperatedByString". Any advice? Thanks

import UIKit
import Parse

class ViewController: UIViewController {


override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}




@IBOutlet weak var username: UITextField!



@IBOutlet weak var password: UITextField!



//Login function. Drag Down keyboard after clicking login
@IBAction func Login(sender: AnyObject) {
username.resignFirstResponder()
password.resignFirstResponder()

var username_array: [String] = username.componentsSeperatedByString(".")


}
}

Answer

The username is a UITextField not String. Try this:

var username_array: [String] = username.text.componentsSeperatedByString(".")
Comments