Struct not being populated

北城以北 提交于 2020-01-25 07:30:08

问题


I have a struct called Item and wish to populate its variables.

struct Item {
    var title: String
    var others: [String]
}

Programs is an array of all the Item’s created.

var Programs = [Item]()

I created a struct Item by the following:

var entered = Item(title: hello1, others: ["hello2"])

The problem is that when I go to print the list of Items, the programs array is empty

print(Programs)

回答1:


You need to append it

print("Before :",programs)
let entered = Item(title: hello1, others: ["hello2"])
programs.append(entered) 
print("After:",programs)


来源:https://stackoverflow.com/questions/59383482/struct-not-being-populated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!