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?

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

Jhoan Arango
Jhoan Arango
14,575 Points

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?

Jhoan Arango
Jhoan Arango
14,575 Points

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.