Created
February 22, 2017 11:04
-
-
Save KyleGoslan/e3a6654547f7fd9f4b1846b770edecd5 to your computer and use it in GitHub Desktop.
Random string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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