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

Need help - Whats wrong in here.

This is my function.

func checkforvalue(#name : String) -> Bool
{
    var teachers = ["Srini", "Amit", "Narayan", "Swamy", "Guru"]
    var found = false

    for n in teachers
    {
        if n == teachers      
        {
            found = true
        }
    }
    return false
}

checkforvalue(name : "Srini")

I have typed it exactly as Amit explained in the video. I throws me an error at (if n==teachers). saying "Cannot invoke '==' with an argument list of type ('string, @String , @value)[String])

:(

Thanks in advance.

2 Answers

Srinivasan, it seems when you are checking if the value is equal to teachers you are comparing two different types of values. If your code, n is a item in the teaches array, a string. You cannot implicitly compare a string to an array so I am guess you are trying to access a value in the array. Try using this code:

func checkforvalue(#name : String) -> Bool
{
    var teachers = ["Srini", "Amit", "Narayan", "Swamy", "Guru"]
    var found = false

    for var i = 0; i < teachers.length; i++ { 

        if name == teachers[i] { 

            found = true
        } 
    }
    return found
}

checkforvalue(name : "Srini")

In your return statement, you returned false so the method will always return false. Instead of always returning false, I think you want to return if the name was found or not. I did this by returning (found), the boolean, so the method will return whether or not that name is found in the array. I also changed the for loop to create an index that will compare the name passed to every object in the array. I hope this helps!

If you have any questions, feel free to ask!

Best Answer. Thanks so much. I figured it before seeing this comment. But my answer was i was comparing n == teachers. It should actually been n == names. Please check my code below.

func checkforvalue(#name : String) -> Bool
{
    var teachers = ["Srini", "Amit", "Narayan", "Swamy", "Guru"]
    var found = false
    for n in teachers
    {
        if n == name      
        {
            found = true
        }
    }
    return false
}

checkforvalue(name : "Srini")

Two things i learned from your example was teachers[i] - Array, which i forgot. Another things was a brush up to my knowledge that i'm trying to compare n == teachers where n is a constant, being compared to array. How stupid I'm.

Tons of thanks. Rgds - Srini

Srinivason, I am glad that I could be of help! Your code looks fine but there is just one thing that you will need to change to make it 100% accurate. At the very end of the function, you are just returning false. Instead, you need to return the value of the variable found so you don't receive false every time you go to check the method. Here is what the correct code should look like:

func checkforvalue(#name : String) -> Bool
{
    var teachers = ["Srini", "Amit", "Narayan", "Swamy", "Guru"]
    var found = false
    for n in teachers
    {
        if n == name      
        {
            found = true
        }
    }

    // The method will now return true or false, depending on if the name is found in the array.
    return found
}

If you have any questions, feel free to ask!

:) you are correct.... proved i was stupid again... :) Thanks Landon