kai
kai

Reputation: 330

change collection optional value in swift?

rowDescriptor is a AnyObject? type, I get convert self.rowDescriptor?.value to images. I want to add image to the self.rowDescriptor?.value. I failed to do it as below. images has one value. but self.rowDescriptor?.value still empty. I can't find any document about it. what's the reason about it.

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

Upvotes: 0

Views: 119

Answers (1)

Kevin
Kevin

Reputation: 17576

The array type in Swift is a struct. Structs in swift are value types not reference types. To clarify:

images contains a copy of self.rowDescriptor?.value

images.append( changes the copy images, not the original value in your rowDescriptor.value

You want to change self.rowDescriptor?.value so just set that to your newly changed images array.

self.rowDescriptor?.value = images

td;dr

To fix, add self.rowDescriptor?.value = images.

Upvotes: 3

Related Questions