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

What does "_" mean?

func searchNames(Name name: String) -> Bool? {
    let names = ["Kamran", "Sevda", "Mama", "Doggy"]

    _ = false

    for _ in names {

    }
    return nil
}

// what dies this "_" mean ? why Xcode advising me to change it?(please ignore my code totally, just focus on "_")
// below my code works normally when I just finished my code despite errors 

func searchNames(Name name: String) -> Bool {
    let names = ["Kamran", "Sevda", "Mama", "Doggy"]

    var found = false

    for n in names {
        if n == name {
            found = true
        }
    }
    return found
}
searchNames(Name: "Kamran")

I just want to know what does it "_mean"

Hello, I did some changes to your question, so that the code can be displayed properly. No changes were made to anything in your question.

1 Answer

Hello:

With Swift 2.0 some changes have emerged, and one of them is the use of the underscore _. What Xcode is trying to say is if you are not going to use a variable or a constant after declaring it inside a method then, just use the underscore.

lets use your code as an example.

func searchNames(Name name: String) -> Bool {
    let names = ["Kamran", "Sevda", "Mama", "Doggy"]

    var found = false // Declared variable

    for n in names {
        if n == name {
            found = true // Variable being used here
        }
    }
    return found // Variable being used here too
}

Since you are using the variable after being declared, then you do not have to use the underscore. But say that you declared that variable but didn't use it after, then Xcode will suggest to use the underscore.

Below I will post the same code but will change a bit, where Xcode will tell me to replace a variable not being used for the underscore.

func searchNames(Name name: String) -> Bool {
    let names = ["Kamran", "Sevda", "Mama", "Doggy"]

    _ = false  // Variable was here, now replaced with underscore since its not used anywhere else. 

    for n in names {
        if n == name {
            print("This is just a test")
        }
    }
    return nil
}

This is the warning and fix it suggestion from Xcode.

Image

Hope this helps you

Good luck