Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Swift Functions and Optionals Optionals What is an Optional?

Christopher Mayfield
Christopher Mayfield
19,928 Points

when I replicate his code, I don't get a struct.

func findApt (aptNumber : String) -> String? {
    let aptNumbers = ["101","202","303","404"]
    for tempAptNumber in aptNumbers {
        if ( tempAptNumber == aptNumber) {
            return aptNumber
        }
    }
 return nil


}


let culprit = findApt("101")
culprit

When i replicate his code I get

"101"

why don't i get a struct?

3 Answers

Yuka Langbuana :

A method is a “function” inside a class or a struct, we no longer call it a function once its inside the class or a struct.

For example :

/// This is a function 

func name(parameter: String) {
       /// function codes in here
}

Once the function is in a struct, is now called a “method”

struct NameOfStruct {
var one = value 
var two = value

func name(parameter: String) {
      /// function codes in here
             }
}

From what I can see on the code on the original question, I only see the function, and I don’t see a struct. His questions is," when I replicate his code, I don't get a struct.” What does he mean by this ? Perhaps the question is not so clear, or he is missing some of the rest of the code, that might be the struct itself.

Yuka Langbuana
Yuka Langbuana
1,826 Points

So do I, and I still can't figure out why. I even downloaded the project files he provided but still, no light

Is it the XCode? or something else? Jhoan Arango , I don't quite understand your explanation, could you explain it more, please?

From what I can see in your code, you have a method, or a function inside a class or a struct. You are missing the rest of the Struct.