skip to Main Content

Hey my code doesnt work it shows problem like this



let password = alphabet.randomElement() + alphabet.randomElement()


print(password)

I am a little begginer of swift and I dont know how to fix it I am searching on the interenet and I cant find anything like that.

Can you help me with fix this problem?

3

Answers


  1. As a beginning Swift developer, read up on Optionals. They will give you fits until you understand them. I suggest downloading and reading Apple’s Swift book for their Books platform (formerly known as iBooks.) You can read their e-books on your Mac or on an iPad. (I don’t recommend it on an iPhone though. The screen is too small.)

    Now, to your question:

    The function randomElement() returns an Optional. That’s because the array could be empty.

    Think of an optional like a box that may be empty or it may contain an object. Optionals can be typed, so an Integer Optional (Int?) is a box that either contains an integer or nothing (nil)

    You have to "unwrap" an optional to see what’s in it.

    There are various ways to do that. For your case, I would recommend using the "nil coalescing operator", ??. That lets you provide a placeholder value to use if the Optional contains nil.

    Consider this code:

    var foo: String? = "bar"
    let newFoo = foo ?? "nil"
    

    The expression foo ?? "nil" will try to unwrap foo. If it contains a string, you get the string. If it contains nil, it returns the value you provide after the ?? – in this case, the String "nil".

    Using that in your code, it would look like this:

    let password = alphabet.randomElement() ?? "" + alphabet.randomElement() ?? ""
    

    So you provide a placeholder empty string for both calls to randomElement(). It should never be nil, but if it is, you just get an empty string.

    There is also something called the "force unwrap" operator, !. I would avoid that like the plague until you really, really, REALLY know what you are doing. (And then you should still try to avoid it.) I call it the "crash if nil" operator, because that’s what it does. If the optional you are unwrapping contains nil, your program crashes.

    Then there are other things like optional binding (if let), guard, and various other ways of handling Optionals that you should learn about and get familiar with.

    Login or Signup to reply.
  2. alphabet.randomElement( ) returns an optional hence one needs to unwrap it first.

    `
    if let randElement = alphabet.randomElement( ) as? String {
       print (randElement + randElement)
    }
    `
    
    Login or Signup to reply.
  3. Solution 1:

    in most cases using "!" means bad code. But in THIS exact case it is ok to use here because of "alphabet" array for sure is not empty:

    let password = alphabet.randomElement()! + alphabet.randomElement()!
    print(password)
    

    But a little bit "better" codestyle is to use sth like this:

    Solution 2:

    guard let rnd1 = alphabet.randomElement(),
          let rnd2 = alphabet.randomElement()
    else { return } // here must be your error handling
    
    let password = "(rnd1)(rnd2)"
    
    print(password)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search