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 Swift Functions and Optionals Optionals What is an Optional?

pleas Help :\ don't Know what is the problem :(

i wrote a code following the same orders in this video but still cannot compile my code i don't Know why !!

//Optional ...


func findRoom (roomNumber: String) -> String? {

    let roomNumber = ["123","234","456","678","891"]

    for avalableRoom in roomNumber {
        if ( avalableRoom == roomNumber ) {
            return roomNumber //=>هذا لأمر يعامل كشرط لأنه جا عشان يطلع لنا رقم موجود فقط اذن اشترطنا عليه حاجة معينة
        }
        return nil
    }

}

let Room = findRoom ("123")
Room!

this is what i wrot and i got this error massege

"Playground execution failed: func3.playground:22:27: error: could not find an overload for '==' that accepts the supplied arguments if ( avalableRoom == roomNumber ) { ~~~~~~~~~~~~~^~~~~~~~~~~~~"

2 Answers

Jhoan Arango
Jhoan Arango
14,575 Points

Hello there:

From the looks of your code you are using too many "roomNumbers" for different things, the compiler will get confused which one to use and for what. Perhaps naming your array constant will fix this issue, also your return statement should be inside the findRoom method and not inside the loop.

func findRoom (roomNumber: String) -> String? {

    let rooms = ["123","234","456","678","891"] // Changed the name of array

    for avalableRoom in rooms { // Used the new array name in the loop
        if avalableRoom == roomNumber  {
        }
    }       // The return has a string value and it's now outside the loop
    return "You Found  Room Number \(roomNumber)" 
}

findRoom("123")

Good luck hope this helps you understand better.

i finally found the mistake it's about the name of my array and the parameter that is specified , But now i'm confused Mr.Amit named his array and parameter and the if statement same name , why did i have to change the names in my Func??

Jhoan Arango
Jhoan Arango
14,575 Points

Hello NOUR :

This is Amit's code.

             // Parameter name is "aptNumber"
func findApt (aptNumber : String) -> String {
    let aptNumbers = ["101","202","303","404"] // Array name is "aptNumbers".

  for tempAptNumber in aptNumbers { // Array being used "aptNumbers".
        if ( tempAptNumber == aptNumber) { // Parameter name being used again "aptNumber". 
            return aptNumber
        }
    }
}

As you can see, they are different names, it may get confusing since he is using plural and singular names. But this is exactly the change we made to your code.

Cheers

hahahahaha , yes it takes a time to recognise especially when English is not your original language , thanks for helping me :)