简单的快速数组追加不起作用 [英] Simple swift array append not working

查看:26
本文介绍了简单的快速数组追加不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这将是非常初级的,但我有这段代码:

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

    let tempLabels = image.label?.allObjects as! [Labels]
    for i in 0..<tempLabels.count{
        labels?.append(tempLabels[i].label!)
    }

}

labels 在公共范围内,所以函数应该可以访问它,但是当循环运行时,labels 仍然是 nil,没有元素.

labels is in the public scope, so the function should have access to it, but when the loop runs through, labels is still nil with no elements.

当我在调试过程中 po 时,tempLabels 就像我期望的那样带有 2 个字符串元素.

When I po during debugging, tempLabels is as I expect it to be with 2 string elements.

我很确定这是一个非常简单的问题,但我想我现在刚刚摆脱它.

I'm pretty sure this is a very simple problem, but I guess I'm just out of it right now.

推荐答案

标签从未被初始化.改变

Labels has never been initialised. Change

var labels:[String]?

var labels:[String] = []

这篇关于简单的快速数组追加不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆