Generating random alphanumeric string in Swift
Hello everyone! I'm writing an iOS app in Swift, and I need to generate random strings consisting of letters (Latin, both cases) and digits. For example, to create unique identifiers or temporary passwords. I'm a beginner, and my current implementation looks like this:
```swift
let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
var randomString = ""
for _ in 0..<10 {
let randomIndex = Int(arc4random_uniform(UInt32(letters.count)))
randomString.append(letters[letters.index(letters.startIndex, offsetBy: randomIndex)])
}
print(randomString)
```
This works, but I feel the code is cumbersome and not very "Swifty". First, `arc4random_uniform` is deprecated in Swift 4.2+, and Xcode gives a warning. Second, constantly calculating the index via `inde
No answers yet.
Log in to leave an answer
Blogs
Introduction to C# in 2025: Where to Start and How Not to Give Up