问题
I recently started learning swift via an online course.
I was given the Task to generate a passwort out of a given Array containing characters. We learned mainly two code examples to randomly choose one.
variable[Int.random(in: 0...25)]
variable.randomElement()
Both work just fine when pulling out one single element out of an array but only "variable[Int.random(in: 0...25)
" when combined multiple times with a plus (+).
Why is that?
I looked up the documentation but couldn't find an answer
https://developer.apple.com/documentation/swift/array/2994747-randomelement
Explanation:
This code works:
let alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
//The number of letters in alphabet equals 26
var password = alphabet[Int.random(in: 0...25)] + alphabet[Int.random(in: 0...25)] + alphabet[Int.random(in: 0...25)] + alphabet[Int.random(in: 0...25)] + alphabet[Int.random(in: 0...25)] + alphabet[Int.random(in: 0...25)]
print(password)
This code does not work, because "randomElement()" gets grey after combining multiple with plus (why?)
let alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
//The number of letters in alphabet equals 26
var password = alphabet.randomElement() + alphabet.randomElement() + alphabet.randomElement() + alphabet.randomElement() + alphabet.randomElement() + alphabet.randomElement()
print(password)
Edit: Thanks for the fast explanation guys !
回答1:
The difference is that randomElement
returns an optional string ( String?
), as opposed to the subscript that returns a non-optional. Why does randomElement
return an optional string? Well, what if the array is empty?
And Swift can't figure out how to add 2 optional strings (let alone 6! So it just gives up). One way to fix this is to force-unwrap the return values of randomElement
:
let password = alphabet.randomElement()! + alphabet.randomElement()! + alphabet.randomElement()! + alphabet.randomElement()! + alphabet.randomElement()! + alphabet.randomElement()!
We know the array is not empty, so we can safely force-unwrap here.
Arguably, randomElement
is the better method to use here, because it forces you to think about the situation where the array is empty, and handle it accordingly. The first approach with subscripts doesn't have checks for whether the array is empty, or whether the indices are correct, etc.
回答2:
This is because of the different return type between the two approaches.
Indexing an array of strings return, as you would expect, the String at that index.
However the .randomElement()
function has a different signature: it return an optional element of the sequence, in your case an optional string (String?).
The '+' operator is defined for strings, but not for optional Strings. To add together the elements returned from .randomElement()
you would need to unwrap them first. Given you have a closed sequence you it would be safe to force unwrap them with '!':
var password = alphabet.randomElement()! + alphabet.randomElement()! + alphabet.randomElement()! + alphabet.randomElement()! + alphabet.randomElement()! + alphabet.randomElement()!
回答3:
beginner in swift with below solution
let passwordChrt:Array = [
"A", "a", "B", "b", "C" , "c", "D", "d", "E", "e", "F", "f", "G", "g", "H", "h",
"I", "i", "J", "j", "K", "k", "L", "l", "M", "m","N", "n", "O", "o", "P", "p",
"Q", "q", "R", "r", "S", "s", "T", "t", "U", "u", "V", "v", "W", "w","X", "x", "Y",
"y", "Z", "z", 1, 2, 3, 4, 5, 6, 7 , 8 ,9, 0, "!", "@", "#", "$", "%", "^", "&", "*"] as [Any]
var password:String = ""
passwordChrt.shuffled()
for _ in passwordChrt {
if password.count < 6 {
password.append("\(passwordChrt.randomElement() ?? <#default value#>)")
}
}
print(password)
回答4:
I too started this course and after having a think about it, I like .randomElement() method instead, and thanks for the answers, It helped me understood why one method work and the other didn't and why. The .randomElement() way makes more sense to me.
来源:https://stackoverflow.com/questions/58524742/swift-array-difference-between-int-randomin-0-25-and-randomelement