Created
August 12, 2015 14:17
-
-
Save skreutzberger/4e295f9d222e797649da to your computer and use it in GitHub Desktop.
generate random email adddress in Swift
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
// returns a random email as string | |
func randomEmail() -> String { | |
let nameLength = randomInt(5, to: 10) | |
let domainLength = randomInt(5, to: 10) | |
let domainSuffixes = ["com", "net", "org", "io", "co.uk"] | |
let name = randomText(nameLength, justLowerCase: true) | |
let domain = randomText(domainLength, justLowerCase: true) | |
let randomDomainSuffixIndex = Int(arc4random_uniform(UInt32(domainSuffixes.count))) | |
let domainSuffix = domainSuffixes[randomDomainSuffixIndex] | |
let text = name + "@" + domain + "." + domainSuffix | |
return text | |
} |
I contribute a little...
extension String {
func getRandomEmail(currentStringAsUsername: Bool = false) -> String {
let providers = ["gmail.com", "hotmail.com", "icloud.com", "live.com"]
let randomProvider = providers.randomElement()!
if currentStringAsUsername && self.count > 0 {
return "\(self)@\(randomProvider)"
}
let username = UUID.init().uuidString.replacingOccurrences(of: "-", with: "")
return "\(username)@\(randomProvider)"
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This doesn't work.