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

In the iOS course where we build a fun facts app: if I want to pull in one specific fact, how would I do that?

How can I modify what we did to pull in just one fact? For example, fact number 2.

I modified the method to take in an Int:

func getOneFact(whichFact: Int) -> String { return facts[whichFact] }

But when I try and pass it a number, I get an error:

let getFact = FunFactModel.getOneFact(2)

Help?

3 Answers

Ah, so I should have noticed this before, but the problem is that you have defined what's called an instance method (a method that belongs to an instance of FunFactModel), but you are calling it as if it were a type method. Given the way you've defined the method, you need to create an instance of FunFactModel. Something like this:

let theModel = FunFactModel() // theModel is an instance of FunFactModel

let getFact = theModel.getOneFact(2)

If instead, you'd prefer to make getOneFact a type method and call it the way you did, you would need to write the method as a type, or static method by simply including the keyword static:

struct FunFactModel {

    let facts = [
        "Ants stretch when they wake up in the morning.",
        "Ostriches can run faster than horses.",
        "Olympic gold medals are actually made mostly of silver."]

    static func getOneFact(whichFact: Int) -> String {
        return facts[whichFact]
    }  
}

let getFact = FunFactModel.getOneFact(2)

Generally speaking, instance methods are a better choice, but you will see both, as both have their place.

I recommend reading more about instance and class methods. The Swift docs are a great place to start.

Happy coding!

-Greg

Hi Adam,

Can you post the rest of your code? It's difficult to diagnose the problem, and I don't think I'm familiar with the particular course you're referencing. Also, please give us a bit more detail on the error you're getting.

At first glance, it looks like you may have a scope issue. Is that the entire method? If so, within the function, Swift has no idea what facts is. You have to either pass the function the array as a second parameter, or declare the array inside the function.

Need to see more code though :)

Best,

-Greg

Greg, Thanks so much for the reply! Here is the struct in the model:

struct FunFactModel {

    let facts = [
        "Ants stretch when they wake up in the morning.",
        "Ostriches can run faster than horses.",
        "Olympic gold medals are actually made mostly of silver."]

   func getOneFact(whichFact: Int) -> String {
        return facts[whichFact]
    }  
}

And here is what I have in the View Controller:

let getFact = FunFactModel.getOneFact(2)

And the error on this line is "cannot convert value of type 'int' to expected argument type 'FunFactModel'"

Again, I am trying to create a method that returns just one act when I specify the number for the array in the model.