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?

Mohammed Hossain
Mohammed Hossain
555 Points

The search function below returns a name if it is found in the array and an empty string if it is not found. Modify the

The search function below returns a name if it is found in the array and an empty string if it is not found. Modify the search function to return an optional instead of just a String. In addition, you have to make sure the function returns a nil if the name is not found in the array.

search.swift
func search(#name: String) -> String {
    let names = ["Doc","Grumpy","Happy","Sleepy","Bashful","Sneezy","Dopey"]
    for n in names {
        if n == name {
            return n
        }
    }
    return ""
}

2 Answers

Srinivasan Senthil
Srinivasan Senthil
2,266 Points

func search(#name: String) -> String { let names = ["Doc","Grumpy","Happy","Sleepy","Bashful","Sneezy","Dopey"] for index in names { if index == name { return index } } return "No Name Found" }

search(name: "John")

Assuming that you understood all about the for in loop function in swift. Just a brush up.

In the above example, you are given an array. which is assigned to a constant "names".

Then you have declared a for in loop where each value in your array is assigned to the value index. you have called it "N" in your example. However both are same. Then the index is checked with the name passed through the function. And if they are equal, then you are asking them to return the "Name". If the name the is not there, then print "Name Not Found"


Now coming to optionals: You can make a variable as an optional. To do that just add "?" to the end of the variable.

So what is it? While accessing optional variable through a method or function: If the optional contains a value, the property, method, or subscript call succeeds, returning the value. if the optional is nil, then property, method, or subscript call returns nil.

Nil means NOT zero. It means there is a value, but nothing as of now.(Some thing Like that :) )

**********************************Your Program***************************************

func search(#name: String) -> String? { let names = ["Doc","Grumpy","Happy","Sleepy","Bashful","Sneezy","Dopey"] for index in names { if index == name { return index } } return "Nil" }

search(name: "John")

Explanation: 1. Add ? at the end of the return datatype. 2. This will make the return type variable as optional value. 3. The last return statement should be mentioned as Return "Nil".

Now initalize the function and see if you get the value or not.

Happy Coding.

Watch the vide once again, you will understand. Trust me.

Mohammed Hossain
Mohammed Hossain
555 Points

I am very lost with this lesson and task and i have no idea what is going :/ any clarifications or comments is appreciated. thank you