Tutorial 2: Checklists - adding another tableView

Hi, I wonder if someone may help? I decided to add another tableView in order to experiment with code and produce a 3 tier checklist.
I copied the checklists tableview and renamed all new objects - all works as it should apart from managing to nest the arrays correctly. The original checklistItem is working with the Checklist array but I cannot figure out why I cannot do the same thing again and nest that into a new array for the new page. I have created a new class for the new array called “Triallist” :-

import UIKit
import Foundation

class Triallist: NSObject, Codable {

var name = “”
var itemsP = Checklists
init(name: String) {
self.name = name
super.init()

}

}
but in my new tableView controller have the following error

     for list in Alist {

let item = Checklists() " Cannot invoke initialiser for type ‘Checklists’ with no arguments"
item.text = “Item for (list.name)”
list.itemsP.append(item)

        }

Any help would be gratefully received.
Many thanks
Paul

One of the first things every programmer needs to learn is how to post code on a computer programming forum. Check out each of the icons along the top of the editor window where you type in your question. Here is how code should look:

class ChecklistViewController: UITableViewController {
    
    var items: [ChecklistItem]
    
    required init?(coder aDecoder: NSCoder) {
        items = [ChecklistItem]()
        
        var item = ChecklistItem()
        item.text = "hello"
        item.checked = false
        items.append(item)
        
        item = ChecklistItem()
        item.text = "world"
        item.checked = false
        items.append(item)
        
        item = ChecklistItem()
        item.text = "goodbye"
        item.checked = true
        items.append(item)
 
        super.init(coder: aDecoder)
        
    }

Compare that to the code you posted. See any differences in the format? See how the code I posted is nice and neat and preserves all the indenting? You need to do the same thing with your code.

let item = Checklists() " Cannot invoke initialiser for type ‘Checklists’ with no arguments"

Can you post your code that defines the Checklists class? I think the error is saying that the Checklists class has an init() method that takes at least one argument, yet you are trying to create a Checklsts object without providing the necessary arguments. Your code does something like this:

class Dog {
    var name: String
    
    init(name: String) {
        self.name = name
    }
    
    func bark() {
        print("woof")
    }
}

let myDog = Dog(name: "Spot")
myDog.bark()

let yourDog = Dog()  //***ERROR***
yourDog.bark()

What version of Xcode are you using?

Also, this code looks suspect:

class Triallist: NSObject, Coding {    
    var name = ""
    var itemsP = Checklists  //What is Checklists?
    
    init(name: String) {
        self.name = name
        super.init()
    }
}

I don’t think you can assign a class name to a variable in Swift. You can read about how to initialize Swift objects here:

https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html#//apple_ref/doc/uid/TP40014097-CH18-ID203

There might be other issues with the code but at the moment, the above appears to be what’s giving you trouble. The question there is what is Checklists. Is that a new class that you added to the project? And if yes, can you provide the code for that class? Or better yet, please upload the full project somewhere and provide a link so that anybody interested can take a look at the project as a whole and tell you what might be going wrong.

Many thanks, I have much to learn!
Best wishes Paul

Thanks fahim, I had changed the original class Checklist to Checklists, duplicated that code and changed the class name for the new addition. Could you please suggest a good place to upload the project?
Many thanks Paul

If you are familiar with Git (or GitHub), you can always upload the project there. Otherwise, you can upload to DropBox as a ZIP file and then provide the DropBox link here. Those are the easiest/simplest solutions I can think of …

Thank you fahim, I will try dropbox.
All the best Paul

This topic was automatically closed after 166 days. New replies are no longer allowed.