-1

What functional programming-ish function (map, reduce, zip) can I use to replace the functionality of this for loop? (array have different objects but same number of elements)

@IBOutlet var imageViews: [UIImageView]!
photoURLs = ["http://...photo1...", "http://...photo2...", "http://...photo3..."]

for (index, photo) in photoURLs.enumerated() {
    imageViews[index].setImageFromString(string: photo.urlString)
}

UPDATE: Most of the comments / answers are appropriate for the first version of this question that I asked. Now, I modified it with my concrete case.

Regarding duplicate question: This question does not address combining two arrays, it refers to copying values from one array to another, exact same indexes, but also putting them through a custom method.

Teodor Ciuraru
  • 3,417
  • 1
  • 32
  • 39
  • Btw, your example code is not valid `swift` `for`. And even you fix syntax it very likely will crash on that subscript assignment line. – user28434'mstep Mar 02 '17 at 11:58
  • Guys, I modified specifying that this is pseudocode. Also, the arrays are different in my concrete example. I need to get the elements from the first one and place them in the exact same position in the second. – Teodor Ciuraru Mar 02 '17 at 12:01
  • Ok, is `array2` already filled with something? – user28434'mstep Mar 02 '17 at 12:02
  • So, you want to move **all elements** from `firstArray`, in **the same order** to `array2`? Then @Adolfo 's comment is what are you looking for. – user28434'mstep Mar 02 '17 at 12:06
  • 1
    @Adolfo - please post your comment as an answer. It is the *correct* answer! – Grimxn Mar 02 '17 at 12:06
  • Guys, Adolfo got it right, but that's because of my mistake. I also need to make call a function on the items. I will update my question. – Teodor Ciuraru Mar 02 '17 at 12:09
  • @Grimxn Post as an answer! ;) – Adolfo Mar 02 '17 at 12:19
  • 1
    Didn't you said `array2`(aka `imageViews`) is empty? If you will try to call method using result of subscript of empty array you will have very bad time. – user28434'mstep Mar 02 '17 at 12:20
  • 1
    @Teodor Ciuraru can you post your actual code instead of pseudo code? including the function you want to call on the elements of your array – Ocunidee Mar 02 '17 at 12:21
  • @Ocunidee it's here. I was stating that is is empty as in it doesn't already have any values on any positions, not that it is `nil`. – Teodor Ciuraru Mar 02 '17 at 12:42
  • suppose you have 5 outlets for imageviews,then you have to first add those imageviews in one array manually, then you can use for (index, photo) in photoURLs.enumerated() { imageViews[index].setImageFromString(string: photo) } – Punit Mar 02 '17 at 12:48
  • you code is not working at all. Why would you have an IBOutlet of an array of ImageViews? Also photo in your for loop is of type String, a string does not have a property called urlString and finally we still don't have the signature of your setImageFromString method – Ocunidee Mar 02 '17 at 12:49
  • 1
    Possible duplicate of [How can I interleave two arrays?](http://stackoverflow.com/questions/34951824/how-can-i-interleave-two-arrays) – Laffen Mar 02 '17 at 13:08
  • 1
    @TeodorCiuraru, `imageViews[index]` where `imageViews == []` (even if it's not `nil`) will result in crash, so you will have a bad time. – user28434'mstep Mar 02 '17 at 13:19
  • @TeodorCiuraru, also `zip`ping with empty array (like in accepted answer) will result in empty array. So if your `imageViews` is actually empty you're doing something wrong over there. – user28434'mstep Mar 02 '17 at 13:26
  • Guys. Sorry for the confusion. The `imageViews` array is NOT nil. @Laffen, this question is not a duplicate of the other. – Teodor Ciuraru Mar 02 '17 at 13:30
  • @TeodorCiuraru, **C'mon, i'm talking (it's like 5th comment) about empty (`[]`) array, and you keep talking about `nil`.** What does `nil` has to do with all of this? – user28434'mstep Mar 02 '17 at 13:48
  • Ok. Nvm. I wasn't pretty attentive because it isn't really my case. Sorry :)) – Teodor Ciuraru Mar 02 '17 at 13:49

4 Answers4

3

Supossing that both array shoud contain the same elements...

let array2 = firstArray
Adolfo
  • 1,862
  • 13
  • 19
  • This is the correct answer for what I asked before. Thank you. But I found out that my problem is a little complex than this and I updated the question. – Teodor Ciuraru Mar 02 '17 at 12:35
3

I can't test my code, but could be something like...

self.imageViews = photoURLs.map()
{ 
    guard let data = NSData(contentsOf: $0), let image = UIImage(data: data)
    {
        return UIImageView(image: nil)
    }

    return UIImageView(image: image)
)

Assuming that photoURLs is a URL array.

Grimxn
  • 22,115
  • 10
  • 72
  • 85
Adolfo
  • 1,862
  • 13
  • 19
2

You can try something like this:

zip(imageViews, photoURLs)
    .forEach { $0.setImageFromString(string: $1.urlString) }

For a detail example:

let views = ["View0", "View1", "View2"]
let urls = ["url0", "url1", "url2"]

zip(views, urls)
    .forEach { print("View: \($0), URL: \($1)") }

// Result: 
View: View0, URL: url0
View: View1, URL: url1
View: View2, URL: url2
beeth0ven
  • 1,857
  • 15
  • 18
1
let flatMappedNumbers = firstArray.flatMap { $0 }
print(flatMappedNumbers)
Punit
  • 1,330
  • 6
  • 13