为什么我的图像没有附加到数组? [英] Why is my image not being appended to the array?

查看:82
本文介绍了为什么我的图像没有附加到数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的模型,包括:

I've a simple model that includes:

var photos: [UIImage]?

我已将文件zombies.jpg作为文件复制到项目中。然后,在Table View Controller中的必需init中,我想加载一个虚拟记录。我可以定义所有String和NSDate属性,但是当我尝试追加照片时:

I've copied the file zombies.jpg as a file into the project. Then, in a required init in a Table View Controller, I want to load a dummy record. I can define all the String and NSDate properties, but when I try to append photos:

dummyRecord1.photos?.append(UIImage(named: "zombies.jpg")!)
print(UIImage(named: "zombies.jpg"))
print(dummyRecord1.photos)

控制台显示图像在那里,但它永远不会进入数组:

The console shows that the image is there, but it never gets into the array:

Optional(<UIImage: 0x7f8fd14b4c40>, {2592, 1936})nil


推荐答案

你已经初始化了照片数组,并且你正在使用条件展开,这意味着如果它不是nil附加但你的照片数组当时为零,所以它不会附加照片零阵列。你应该做这样的事情 var照片:[UIImage] = []

You have initialize photos array, and you are using conditional unwrapping, which means if its not nil append but your photos array is nil at the time so it will not append a photo in nil array. You should do something like this var photos:[UIImage] = []

这篇关于为什么我的图像没有附加到数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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