Type has no member error?

I am working on the guided project in Chapter 4 of App Developement with Swift. I have entered the following code from the tutorial:


override func viewDidLoad() {

super.viewDidLoad()

if let savedToDos = ToDo.loadToDos() {

todos = savedToDos

} else {

ToDo.loadSampleToDos()

}

}


I get a Type 'ToDo' has no member 'loadToDos' and a Type 'ToDo' has no member 'loadSampleToDos' error and have searched for an answer but have found nothing that seems to apply. Could someone direct me to where I should look or suggest a solution? Thanks in advance.

Answered by Claude31 in 277644022

When it's obvious we often miss it.


Good luck and don't forget to close the thread.

Have you defined these functions as static ?


static func loadSampleToDos() -> [ToDo] {

Yes I am following the example exactly. I have tried moving the code around but no luck. I had it working until some of the latest updates.

Can you post the complete file (including ToDo struct definition) ?

Can you post your code ?

I tried this successfully in playground (XCode 9.2ß)


struct ToDo {
    var title: String
    var isComplete: Bool
    var dueDate: Date
    var notes: String?

    static func loadToDos() -> [ToDo]?  {          // FUNC MUST BE INSIDE struct
        return nil
    }

   static func loadSampleToDos() -> [ToDo] {         // FUNC MUST BE INSIDE struct
        let todo1 = ToDo(title: "ToDo One", isComplete: false,
                         dueDate: Date(), notes: "Notes 1")
        let todo2 = ToDo(title: "ToDo Two", isComplete: false,
                         dueDate: Date(), notes: "Notes 2")
        let todo3 = ToDo(title: "ToDo Three", isComplete: false,
                         dueDate: Date(), notes: "Notes 3")
        return [todo1, todo2, todo3]
    }
}

var todos = [ToDo]()

if let savedToDos = ToDo.loadToDos() {     // THERE WAS A TYPO IN TUTORIAL: space missing after = sign
    todos = savedToDos
} else {
   todos = ToDo.loadSampleToDos()              // I changed this by adding todos = 
}
print(todos)

This is what I have so far. I tried the code Claude used in Playground in my app but it didn't work. I get a warning with this current code Result of call to 'loadSampleToDos()' is unused and a blank screen when I run it in the simulator. I really appreciate everyone's help.


In the ToDo File:

import UIKit


struct ToDo {

var title: String

var isComplete: Bool

var dueDate: Date

var notes: String?


static func loadToDos() -> [ToDo]? {

return nil

}


static func loadSampleToDos() -> [ToDo] {

let todo1 = ToDo(title: "ToDo One", isComplete: false,

dueDate: Date(), notes: "Notes 1")

let todo2 = ToDo(title: "ToDo Two", isComplete: false,

dueDate: Date(), notes: "Notes 2")

let todo3 = ToDo(title: "ToDo Three", isComplete: false,

dueDate: Date(), notes: "Notes 3")

return [todo1, todo2, todo3]

}

}



In the ToDoTableViewController:


import UIKit


class ToDoTableViewController: UITableViewController {


var todos = [ToDo]()


override func viewDidLoad() {

super.viewDidLoad()

if let savedToDos = ToDo.loadToDos() {

todos = savedToDos

} else {

ToDo.loadSampleToDos()

}

}


override func tableView(_ tableView: UITableView,

numberOfRowsInSection section: Int) -> Int {

return todos.count

}


override func tableView(_ tableView: UITableView, cellForRowAt

indexPath: IndexPath) -> UITableViewCell {

guard let cell = tableView.dequeueReusableCell(withIdentifier:

"ToDoCellIdentifier") else {

fatalError("Could not dequeue a cell")

}

let todo = todos[indexPath.row]

cell.textLabel?.text = todo.title

return cell

}

}

So now the code compiles ? Exact ?


The problem you have is the one I notcied in my post :

   todos = ToDo.loadSampleToDos()              // I changed this by adding todos = 


You have to use the result of the func somewhere !


So I store it in todos.

I feel stupid. I should have noticed that. I have done plenty of coding in the past but Swift is new to me. The code in the book left that off so I assumed it was correct. Thanks so much for the help Claude.

Accepted Answer

When it's obvious we often miss it.


Good luck and don't forget to close the thread.

Hello. I am still getting the same error message.


In the ToDoTableViewController:


import UIKit


class ToDoTableViewController: UITableViewController {


var todos = [ToDo]()


override func viewDidLoad() {

super.viewDidLoad()

if let savedToDos = ToDo.loadToDos() {

todos = savedToDos

} else {

ToDo.loadSampleToDos()

}

}


Please help with the error messge: Type "TODO' has no member "loadToDos'


Type "ToDo' has no member 'loadSampleToDos'

Type has no member error?
 
 
Q