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 trialDaniel Mendez
1,792 PointsAssign the document FactBook.swift to a constant
I cant assign the document FactBook.swift to the let factBook statment it prints an error "Unresolved identifier FactBook"
Daniel Mendez
1,792 Pointsimport UIKit
class ViewController: UIViewController {
@IBOutlet weak var factLabel: UILabel!
let factBook = FactBook()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
factLabel.text = factBook.factsArray[1]
}
3 Answers
Stone Preston
42,016 Pointsyou dont have your FactBook.swift code inside the struct. all that code needs to be inside a struct named FactBook
// FactBook.swift
// Fact Game
//
// Created by Daniel Mendez on 12/20/14.
// Copyright (c) 2014 Daniel Mendez. All rights reserved.
//
import Foundation
// put code inside struct
struct FactBook {
let factsArray = [
"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."
]
func randomFact() {
var unasignedArrayNumber = (UInt32(factsArray.count))
var unasignedRandomNumber = arc4random_uniform(unasignedArrayNumber)
var randomNumber = Int(unasignedRandomNumber)
return factsArray[randomNumber]
}
// make sure and close the struct with a closing curly brace
}
make the above changes and save the file. your code should work after that.
the reason it was not working before is the line
let factBook = FactBook()
initializes an instance of the Struct FactBook that should be defined in FactBook.swift, however you did not have the struct in there so it did not recognize it
Daniel Mendez
1,792 Points// FactBook.swift // Fact Game // // Created by Daniel Mendez on 12/20/14. // Copyright (c) 2014 Daniel Mendez. All rights reserved. //
import Foundation
let factsArray = [ "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." ]
func randomFact() { var unasignedArrayNumber = (UInt32(factsArray.count)) var unasignedRandomNumber = arc4random_uniform(unasignedArrayNumber) var randomNumber = Int(unasignedRandomNumber)
return factsArray[randomNumber]
}
I already have the document with all the code, in the ViewContrller.swift the code recognizes the facts array but not the document.
Daniel Mendez
1,792 PointsThanks :)
Stone Preston
42,016 PointsStone Preston
42,016 Pointspost your code please