10

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?

Hermann Klecker
  • 14,039
  • 5
  • 48
  • 71
Aldo Lazuardi
  • 1,898
  • 2
  • 19
  • 34
  • Mate , please be clear on your question. Can you explain "asset in photos" – Mr. Bean Jan 20 '16 at 11:38
  • wait i'll update it, thank you – Aldo Lazuardi Jan 20 '16 at 11:40
  • Does this answer your question? [Find an item and change value in custom object array - Swift](https://stackoverflow.com/questions/38084406/find-an-item-and-change-value-in-custom-object-array-swift) – Jeff Jun 26 '20 at 21:26

1 Answers1

14
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.

Hermann Klecker
  • 14,039
  • 5
  • 48
  • 71
  • what if i didn't know the array number? could i change it based on userId? (img3) – Aldo Lazuardi Jan 20 '16 at 11:56
  • Yes. You can iterate though the array and stop where the userId is the same as the one that your are looking for or you use a dictionary instead of the array and access the object directly. See my answer. It is all in there. – Hermann Klecker Jan 20 '16 at 12:00
  • hi @HermannKlecker thank you for ur answer, i've another question that related to this page. feel free to see it http://stackoverflow.com/questions/34915370/update-change-array-value-from-another-viewcontroller-swift – Aldo Lazuardi Jan 21 '16 at 04:28
  • Iteration through long list using for is very inefficient. I would use hasmap – Yaroslav Dukal Feb 13 '18 at 22:20
  • What do you mean by "how to save it"? You would save it as you save any other array. And how to save it depends very much on your persistence storage. – Hermann Klecker May 26 '18 at 10:22