Create random alphanumeric string of desired length in swift
//
// Extensions.swift
//
// Created by Harshad Kale on 1/23/16.
//
import Foundation
extension String {
/**
Create a random alphanumeric string of desired length
- Parameter length: Desired length of the string to be generated
- Returns: Random alphanumeric string of length `length:`
*/
static func randomAlphaNumericString(length : Int) -> String {
var characters = Array(48...57).map {String(UnicodeScalar($0))}
characters.appendContentsOf(Array(65...90).map {String(UnicodeScalar($0))})
characters.appendContentsOf(Array(97...122).map {String(UnicodeScalar($0))})
var randomString = String(length)
for (var i = 0; i < length; i++){
let length = UInt32(characters.count)
let randonIndex = Int(arc4random_uniform(length))
randomString += characters[randonIndex]
}
return randomString
}
}