create a password with numbers in swift

To create a password with numbers in Swift, you can use the arc4random_uniform() function to generate a random number between 0 and 9, and append it to a string. Repeat this process for as many characters as you want in your password.

Here's an example function that generates a password string with a specified length (in this case, 8 characters):

main.swift
func generatePassword(length: Int) -> String {
    var password = ""
    for _ in 1...length {
        let digit = arc4random_uniform(10)
        password += String(digit)
    }
    return password
}
200 chars
9 lines

You can call this function with the desired length of your password like this:

main.swift
let password = generatePassword(length: 8)
print(password) // Example output: "02379148"
89 chars
3 lines

Note that this is just a simple example of generating a random password with only digits. For more secure passwords, you should consider using a combination of uppercase and lowercase letters, as well as other special characters.

gistlibby LogSnag