-
-
Save TheMuellenator/9fdb0d41d6343218ca1bcb6fb724949e to your computer and use it in GitHub Desktop.
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) |
let alphabetArray = ["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"]
var password = ""
for l in 0..<6 {
password += alphabetArray.randomElement() ?? ""
}
print(password)
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"]
var password = alphabet.randomElement()!+alphabet.randomElement()!+alphabet.randomElement()!+alphabet.randomElement()!+alphabet.randomElement()!+alphabet.randomElement()!
print(password)
I really know I am late, I've all the top solutions on this page and I really don't get what does "!" mean in alphabet.rendomElemnet()! I've been looking all over internet (in my capacity) but still haven't found a reasonable solution. I hope you guys can help me on this . Thanks
Types, say String
or Int
, have their variant type called Optional
(Optional String
written in code as String?
, Optional Int
written in code as Int?
).
A String?
can either be a String
or a null value (called nil
in Swift) that means "nothing".
Why do we have that? Let's say you have a program where you can define nicknames for your users, you could give it the String?
type as not everybody necessarily have a nickname, so the value of a nickname
variable could be either a String
, either nothing (nil
) if no nickname would have been defined for that user.
Now let's say you have the following function that greets your users using their nickname
func greet(personNickname: String) -> String {
let greeting = "Hello, " + personNickname + "."
return greeting
}
It expects the nickname as a parameter being a String
and not a String?
. To call your function, you could not do:
print(greet(user.nickname))
since nickname is a String?
and the function expects a String
, you would have a compilation error.
To effectively pass a String
to your function, you would use the !
symbol that says: "I know there's a value in there, trust me and act as if it was not 'Optional'." :
print(greet(user.nickname!))
Looking at the randomElement
method of the Array
class in the documentation, we can see that whenever we call it, it returns a value of the type Element?
:
func randomElement() -> Self.Element?
Our array alphabet
being an array of String
elements, calling randomElement
on it would return the type String?
.
Since our password is of type String
and not String?
, it should be composed of String
s, so using the randomElement
method, we should transform the String?
values into String
ones by using the !
symbol/operator.
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"];
var password = "";
for i in 0...5 {
password += alphabet.randomElement() ?? "";
}
print(password);
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 a = alphabet.randomElement()
var b = alphabet.randomElement()
var c = alphabet.randomElement()
var d = alphabet.randomElement()
var e = alphabet.randomElement()
var f = alphabet.randomElement()
var result = ""
result.append(a!)
result.append(b!)
result.append(c!)
result.append(d!)
result.append(e!)
result.append(f!)
let password = result
print(password)
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"]
var password = ""
for _ in 1 ... 6 {
password += alphabet.randomElement()!
}
print(password)
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"]
let randomElements = Array(alphabet.shuffled().prefix(6))
let password = randomElements.joined()
print (password)
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"]
var password = ""
while password.count != 6 {
password = password + alphabet[Int.random(in: 0 ... alphabet.count - 1)]
}
print(password)
func exercise() {
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"];
let password = alphabet.shuffled().prefix(6).joined()
print(password);
}
func exercise() {
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"]
}