Checklist Project: It is showing me 5 to 6 items within the 2 checklists I created

    var list = Checklist(name: "Hey Buddy")
    dataModel.lists.append(list)
    
    list = Checklist(name: "What up peep")
    dataModel.lists.append(list)
    
    
    
        
    for list in dataModel.lists {
        var item = Checklistitem()
        item.text = "Item for the Shit"
        dataModel.lists[0].items.append(item)
        
        item = Checklistitem()
        item.text = "Item for Cool Apps"
        dataModel.lists[1].items.append(item)
        
    }
  
}

What’s the point of this:

for list in dataModel.lists

if you just write:

dataModel.lists[0]

Do you know what a for-loop does?

Here’s an example of how you can use a for-loop:

class DataModel {
    var lists = [
        ["a", "b" ,"c"],
        ["d", "e", "f"]
    ]
}

var dataModel = DataModel()

for list in dataModel.lists {
    print(list)
    print("---")
}

--output--:
["a", "b", "c"]
---
["d", "e", "f"]
---

Your question is sort of like asking why the following loop doesn’t print out all the lists:

for list in dataModel.lists {
    print(dataModel.lists[0])
    print("---")
}

--output:--
["a", "b", "c"]
---
["a", "b", "c"]
---

Note that the loop never uses list, and in fact the compiler will warn you:

‘list’ was never used.

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