Tutorial 2 - Error at super.init(coder: aDecoder)

Hello,

Working my way through the 2nd Tutorial and I’m coming across the following error when trying to call the super. “Property ‘self.row0item’ not initialized at super.init call”. Code below. Can someone point me in the right direction?

Thank you,
Darin

var items: [ChecklistItem]

required init?(coder aDecoder: NSCoder) {
    items = [ChecklistItem]()
    
    let row0item = ChecklistItem()
    row0item.text = "Walk the dog"
    row0item.checked = false
    items.append(row0item)
    
    let row1item = ChecklistItem()
    row1item.text = "Brush my teeth"
    row1item.checked = true
    items.append(row1item)
    
    let row2item = ChecklistItem()
    row2item.text = "Learn iOS development"
    row2item.checked = true
    items.append(row2item)
    
    let row3item = ChecklistItem()
    row3item.text = "Soccer practice"
    row3item.checked = false
    items.append(row3item)
    
    let row4item = ChecklistItem()
    row4item.text = "Eat ice cream"
    row4item.checked = true
    items.append(row4item)
    
    super.init(coder: aDecoder) //  Error: Property 'self.row0item' not initialized at super.init call
}

You have a line of code in that init function starting let row0item. This is a local variable - it only exists inside the init function. It’s not setting a value to the property called row0item in your type. To do that, you either need to remove the let, so you’re only working with the property, or you need to create the local row0item variable and assign the result to the type property afterwards (before the super.init) with self.row0item = row0item (the ‘self’ is necessary to disambiguate between the two identically named variables).

3 Likes

Thank you! This solved my problem. So it was scope that killed me. :frowning: