pscbonline-ios/PSCBOnline/Sources/Extension/String.swift

27 lines
651 B
Swift
Raw Permalink Normal View History

2024-07-08 15:20:00 +03:00
//
// 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
}
}