Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Levi Kline
Levi Kline
2,825 Points

What does this code mean?

I had a long code using if statements to return based on if something was left empty. Then, my friend gave me this code:

func hasFilledPassword(text: String = "") -> Bool {
    let text = text
    return !(text == "")
}

what does the last part mean, please explain

Let me make parts of it

Part-1: text == "" means it will compare text with empty string. If it is empty then it will return true else false.

Part-2: ! will make true to false and vice versa.

so lets consider below scenario

  1. if your text has empty string then !(text == "") will become !(true) and it will return false.

  2. if your text has some string then !(text == "") will become !(false) and it will return true.