Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

update/change array value (swift)

Data Model

class dataImage {
    var userId: String
    var value: Double
    var photo: UIImage?
    var croppedPhoto: UIImage?

init(userId:String, value: Double, photo: UIImage?, croppedPhoto: UIImage?){
    self.userId = userId
    self.value = value
    self.photo = photo
    self.photo = croppedPhoto
   }

}

View Controller

var photos = [DKAsset]() //image source
var datas = [dataImage]()

var counter = 0
    for asset in photos{
        asset.fetchOriginalImageWithCompleteBlock({ image, info in // move image from photos to datas

            let images = image
            let data1 = dataImage(userId: "img\(counter+1)", value: 1.0, photo: images, croppedPhoto: images)
            self.datas += [data1]
            counter++

        })
    }

from that code, let's say i have 5 datas:

 - dataImage(userId: "img1", value: 1.0, photo: images, croppedPhoto:
   images)
 - dataImage(userId: "img2", value: 1.0, photo: images, croppedPhoto:
   images)
 - dataImage(userId: "img3", value: 1.0, photo: images, **croppedPhoto:
   images**)
 - dataImage(userId: "img4", value: 1.0, photo: images, croppedPhoto:
   images)
 - dataImage(userId: "img5", value: 1.0, photo: images, croppedPhoto:
   images)

How to change/update img3's croppedImage value?

like image 934
Aldo Lazuardi Avatar asked Jan 20 '16 11:01

Aldo Lazuardi


People also ask

How to change an item in an array in Swift?

To replace an element with another value in Swift Array, get the index of the match of the required element to replace, and assign new value to the array using the subscript.

How do you change the value of an array?

To change the value of all elements in an array:Use the forEach() method to iterate over the array. The method takes a function that gets invoked with the array element, its index and the array itself. Use the index of the current iteration to change the corresponding array element.

What is map function in Swift?

The map() Function in Swift. In Swift, you can use the built-in map() function to modify each element in a collection. The map() function loops through the collection, applying a function for each element. The map() function always returns an array where the transformed values are.


1 Answers

self.datas[2] = dataImage(userId: "img6", value: 1.0, photo: images, croppedPhoto:  images)

This will replace the 3rd object in the array with a new one.

or

self.datas[2].value = 2.0

This will change the value of the dataImage object with userId "img3".

Does this answer your question?

If you need to search for a specific value in userId, then you are far better of with a dictionary (associated array) rather than an (indexed) array.

var datas = [String, dataImage]()
...
self.datas["img\(counter+1)"] = ...

And you access it the same way.

self.datas["img3"].value = 2.0

And please rename the class imageData into ImageData. Class names start with capitals.

like image 64
Hermann Klecker Avatar answered Oct 01 '22 13:10

Hermann Klecker