27 lines
651 B
Swift
27 lines
651 B
Swift
//
|
|
// String.swift
|
|
//
|
|
//
|
|
// Created by AntonovIA on 12.10.2020.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
extension String {
|
|
|
|
private static let alphanumeric = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
|
|
|
static func random(length: Int, allowedChars: String? = nil) -> String {
|
|
let seq: String = ((allowedChars == nil) ? alphanumeric : allowedChars!) as String
|
|
var rnd: String = ""
|
|
|
|
for _ in 0..<length {
|
|
let randInt = arc4random_uniform(UInt32(seq.count))
|
|
rnd += "\(seq[seq.index(seq.startIndex, offsetBy: Int(randInt))])"
|
|
}
|
|
|
|
return rnd
|
|
}
|
|
|
|
}
|