Simple swift array append not working

后端 未结 3 1606
我寻月下人不归
我寻月下人不归 2020-12-11 16:38

I know this is going to be super elementary, but I have this piece of code:

var labels: [String]?

func initVC(image: Images){
    self.image = image

    le         


        
相关标签:
3条回答
  • 2020-12-11 17:15

    Labels has never been initialised. Change

    var labels:[String]?
    

    to

    var labels:[String] = []
    
    0 讨论(0)
  • 2020-12-11 17:17

    You are declaring the labels variable but never allowing it to store information. This means that it does not necessarily exist, since it is not initialized, and therefore cannot be used.

    For it to be useable, you must initialize it

    var labels:[String] = []
    
    0 讨论(0)
  • 2020-12-11 17:31

    Yep, it was super simple.

    Changed

    var labels: [String]?
    

    To

    var labels = [String]()
    
    0 讨论(0)
提交回复
热议问题