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 trialErman Sahin Tatar
3,223 Points" Class ViewController has no initializers " I am getting this error in my code (the same code with video)
firstly, I am writing the same code with video and the code is giving error, I realized that it is logical because our ViewController class has no init method. But it must have.
secondly, when I write let factModel: FactModel() it is giving error, when I get rid of parenthesis . it says okay like ( let factModel: FactModel )
8 Answers
Steven Deutsch
21,046 PointsHey Erman Sahin Tatar, Did you create your FactModel as a Swift File or a Cocoa Touch Class? It needs to be a Swift File.
// To create an instance of your FactModel structure, you need to write:
let factModel = FactModel()
Could you post your code for your ViewController and your FactModel so that I can try and help you?
Steven Deutsch
21,046 PointsHey Erman Sahin Tatar,
@IBOutlet weak var funFactLabel: UILabel!
// Change this
let factModel: FactModel
// To this
let factModel = FactModel()
override func viewDidLoad() {
super.viewDidLoad()
funFactLabel.text = factModel.facts[0]
}
Then let me know what's still not working
Erman Sahin Tatar
3,223 Pointsstruct FactModel {
let facts = ["Ants stretch when they wake up in the morning",
"Ostritches can run faster than horses",
"Olympic gold medals are actually made mostly of silver",
"Your are born with 300 bones; by the time you are adult you will have 206",
"Some bamboo plants can grow almost a meter in just one day",
"The state florida is bigger than England",
"Some penguins can leap 2-3 meters out of the water"]
}
Erman Sahin Tatar
3,223 PointsI know normally I did like you but it gave me another error (let factModel = FactModel() giving error )
Erman Sahin Tatar
3,223 PointsThe main problem is Every class must have init method but here we haven't like in the video but how the code in video can work because there is no init method in ViewController class
Erman Sahin Tatar
3,223 PointsGod! thanks it is about equal sign thank you very much!
Steven Deutsch
21,046 PointsNo problem. I thought that's what it was!
Arman Arutyunov
21,900 PointsI also have an error and plus my simulator doesn't show anything. Just clean white screen.
I can show the code
This is my FactsModel.swift
//
// FactsModel.swift
// FunFacts
//
// Created by Arman on 14/03/16.
// Copyright © 2016 Treehouse. All rights reserved.
//
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." ]
}
And this is ViewController.swift
//
// ViewController.swift
// FunFacts
//
// Created by Arman on 13/03/16.
// Copyright © 2016 Treehouse. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var funFactLabel: UILabel!
let factModel = factModel()
override func viewDidLoad() {
super.viewDidLoad()
funFactLabel.text = factModel.facts[0]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func showFunFact() {
funFactLabel.text = factModel.facts[1]
}
}
Actually I have an error in ViewController.swift on line 14
let factModel = factModel()
which says "Variable used within its own initial value"
Does anybody know what does it mean? Pasan's code is the same but he doesn't have an error and the simulator works fine
Nicholai Hansen
2,395 PointsArman,
Try changing --> let factModel = factModel() TO factModel = FactModel() <-- You want to make sure the struct, FactModel, is capitalized so that when you assign the constant, factModel, to the strings inside of the struct, they don't read as exactly the same value.
Arman Arutyunov
21,900 Pointsalready got this, thank you)
Erman Sahin Tatar
3,223 PointsErman Sahin Tatar
3,223 PointsYes, I created as a swift file, but interesting point is that compiler true class must have init method inside them. I cannot understand the code in the video should not work too.
Erman Sahin Tatar
3,223 PointsErman Sahin Tatar
3,223 Pointsimport UIKit
class ViewController: UIViewController {
}