简体   繁体   English

为什么我的图像没有附加到数组?

[英]Why is my image not being appended to the array?

I've a simple model that includes: 我有一个简单的模型,包括:

var photos: [UIImage]?

I've copied the file zombies.jpg as a file into the project. 我已将文件zombies.jpg作为文件复制到项目中。 Then, in a required init in a Table View Controller, I want to load a dummy record. 然后,在Table View Controller中的必需init中,我想加载一个虚拟记录。 I can define all the String and NSDate properties, but when I try to append photos: 我可以定义所有String和NSDate属性,但是当我尝试追加照片时:

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

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. 你已经初始化了照片数组,并且你正在使用条件展开,这意味着如果它不是nil附加但你的照片数组当时为零,所以它不会在nil数组中附加照片。 You should do something like this var photos:[UIImage] = [] 你应该做这样的var photos:[UIImage] = []

Better do this: 这样做更好:

var photos : [UIImage?] = []

It initializes an empty array, that is ready to store UIImage objects. 它初始化一个空数组,准备存储UIImage对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM