Skip to content

Instantly share code, notes, and snippets.

@KyleGoslan
Created February 22, 2017 11:04
Show Gist options
  • Save KyleGoslan/e3a6654547f7fd9f4b1846b770edecd5 to your computer and use it in GitHub Desktop.
Save KyleGoslan/e3a6654547f7fd9f4b1846b770edecd5 to your computer and use it in GitHub Desktop.
Random string
extension String {
static func randomString(length: Int) -> String {
let letters : NSString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
let len = UInt32(letters.length)
var randomString = ""
for _ in 0 ..< length {
let rand = arc4random_uniform(len)
var nextChar = letters.character(at: Int(rand))
randomString += NSString(characters: &nextChar, length: 1) as String
}
return randomString
}
func validEmail() -> Bool {
let emailRegex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"
let emailText = NSPredicate(format:"SELF MATCHES [c]%@",emailRegex)
return (emailText.evaluate(with: self))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment