Skip to content

Instantly share code, notes, and snippets.

@preble
Created February 7, 2015 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save preble/e21def24603495a95f9c to your computer and use it in GitHub Desktop.
Save preble/e21def24603495a95f9c to your computer and use it in GitHub Desktop.
Generate random identifiers in the style of Xcode.
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