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 Build a Simple iPhone App with Swift 2.0 Structs as Data Models Structs or Classes

Emmanuel Darmon
Emmanuel Darmon
6,115 Points

facts on type "FactsModel"

I got this error: Instance member 'facts' cannot be use on type "FactsModel"

(Of course, I triple checked and my code are the same as in the video)

5 Answers

Hi Emmanuel,

Can you post your code where you are using facts on a FactsModel - the whole file is fine.

Thanks,

Steve.

Emmanuel Darmon
Emmanuel Darmon
6,115 Points

The problem is fixed now, but it's weird, I really didn't change nothing:

The error was pointing this line funFactLabel.text = factModel.fact[1] with a little grey arrow under the "f" of factModel.fact[1]

So I tried to touch around... I change the "factModel.fact[1]" with "FactModel.fact[1]"... still got the error of course... then I changed back to "factModel.fact[1]" and now it's working! :) But i really didn't change nothing, how comes?

Seems odd - maybe the compiler was catching up with itself. Glad you got it working!

Steve.

Emmanuel Darmon
Emmanuel Darmon
6,115 Points

Ok, good to know we can face some Xcode "bugs" like this... Thanks for your help Steve Hunter !

Yeah, soetimes the compiler has 'a moment' and it can throw errors. If you go to Product | Clean that makes it have a proper look at the project and should eliminate any errors like that.

Emmanuel Darmon
Emmanuel Darmon
6,115 Points

Good tricks to know! Thanks.

Ahmet GULER
Ahmet GULER
7,181 Points

Hello I have this exact same problem. Getting the error: Instance member 'facts' can not be used on type 'FactModel'. (A red exclamation mark.)

Here is my code:

class ViewController: UIViewController {

    @IBOutlet weak var label: UILabel!
    @IBAction func ShowLove() {
    label.text = FactModel.facts[1] 
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        label.text  = FactModel.facts[0]
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

and in FactModel.swift file is:

struct FactModel {
    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.",
        "You are born with 300 bones; by the time you are an adult you will have 206.",
        "It takes about 8 minutes for light from the Sun to reach Earth.",
        "Some bamboo plants can grow almost a meter in just one day.",
        "The state of Florida is bigger than England.",
        "Some penguins can leap 2-3 meters out of the water.",
        "On average, it takes 66 days to form a new habit.",
        "Mammoths still walked the Earth when the Great Pyramid was being built." ]
}

In the course, you should have created a member variable, called factModel, that is an instance of FactModel and call the facts property on hat like, factModel.facts[0]. Have you tried doing that?

Steve.

Ahmet GULER
Ahmet GULER
7,181 Points

i closed the x-code and re-opened it. and the error was fixed. i have no idea why it occurred at the first place. Didn't changed anything in code.. it was fixed by just rebooting the x-code application.

Ahmet

Yeah, it can work like that. As in my answerd above, cleaning the project or restarting Xcode can get everything working again sometimes.

Glad you got it sorted. :+1:

Steve.

Ahmet GULER
Ahmet GULER
7,181 Points

Actually, i when i rebooted x-code program the red error signs were gone. i was thought that problem was solved. and wrote here my reply that everything was OK.

However later when i hit the Play icon to run the program and try test it it, it wrote "built failed" and the same error message came back.. Then i paid attention to my code and your previous reply. you were right i was never created the instance inside the class, where the override function resides.

As you have mentioned, After creating the instance 'let factModel = FactModel()' the problem was solved.

I wanted to mention this for those who may search the answer for this problem in this topic.

Thanks for the help

Remove the capital 'F', Martina ... use factModel.facts[0] not FactModel.facts[0] - so you're using the instance, not the class itself.

Make sense?

Steve.

No problem! :+1: