Created
February 7, 2015 14:30
-
-
Save preble/e21def24603495a95f9c to your computer and use it in GitHub Desktop.
Generate random identifiers in the style of Xcode.
This file contains hidden or 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
public class IdentifierGenerator { | |
private let characters = Array("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") | |
private let separator = "-" | |
public func generateIdentifier() -> String { | |
let numCharacters = UInt32(characters.count) | |
func randomCharacter() -> Character { | |
let index = Int(arc4random_uniform(numCharacters)) | |
return characters[index] | |
} | |
var identifier: String = "" | |
identifier.reserveCapacity(3+1+2+1+3) | |
for _ in 1...3 { identifier.append(randomCharacter()) } | |
identifier += separator | |
for _ in 1...2 { identifier.append(randomCharacter()) } | |
identifier += separator | |
for _ in 1...3 { identifier.append(randomCharacter()) } | |
return identifier | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment