快速更改集合可选值?

Posted

技术标签:

【中文标题】快速更改集合可选值?【英文标题】:change collection optional value in swift? 【发布时间】:2015-12-19 03:18:10 【问题描述】:

rowDescriptorAnyObject? 类型,我将self.rowDescriptor?.value 转换为images。我想将图像添加到self.rowDescriptor?.value。我没有做到如下。 images 有一个值。但self.rowDescriptor?.value 仍然是空的。我找不到任何关于它的文件。这是什么原因。

    var images :[UIImage] = self.rowDescriptor?.value as! [UIImage]
    if let image = editedImage 
        images.append(image)
     else 
        images.append(originalImage!)
    

【问题讨论】:

【参考方案1】:

Swift 中的数组类型是结构体。 swift 中的结构是值类型而不是引用类型。澄清一下:

images 包含self.rowDescriptor?.value 的副本

images.append( 更改副本 images,而不是 rowDescriptor.value 中的原始值

您想更改 self.rowDescriptor?.value,因此只需将其设置为新更改的 images 数组即可。

self.rowDescriptor?.value = images

td;博士

要修复,请添加self.rowDescriptor?.value = images

【讨论】:

以上是关于快速更改集合可选值?的主要内容,如果未能解决你的问题,请参考以下文章