Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map modify array of objects in Swift 2.2 (3.0)

I want to be able to modify my array of objects using map in Swift of the fly, without looping through each element.

Before here were able to do something like this (Described in more details here:

gnomes = gnomes.map { (var gnome: Gnome) -> Gnome in
    gnome.age = 140
    return gnome
}

Thanks for Erica Sadun and others, new proposals have gone through and we're now getting rid of C-style loops and using var inside the loop.

In my case I'm first getting a warning to remove the var in then an error my gnome is a constant (naturally)

My question is : How do we alter arrays inside a map or the new styled loops for that matter to be fully prepared for Swift 3.0?

like image 353
kernelpanic Avatar asked Mar 28 '16 11:03

kernelpanic


2 Answers

If you want to keep that syntax, just use a (mutable) temporary variable

gnomes = gnomes.map { (gnome: Gnome) -> Gnome in
  var mutableGnome = gnome
  mutableGnome.age = 140
  return mutableGnome
}
like image 189
vadian Avatar answered Sep 19 '22 14:09

vadian


Given:

struct Gnome {
    var age: Int = 0
}

var gnomes = Array(count: 5, repeatedValue: Gnome())

... there are two decent options. The first is as @vadian put it:

gnomes = gnomes.map{
    var gnome = $0
    gnome.age = 70
    return gnome
}

Whilst the second keeps control over "ageing" private and simplifies mapping at the point of call:

struct Gnome {
    private(set) var age: Int = 0

    func aged(age: Int) -> Gnome {
        var gnome = self
        gnome.age = age
        // any other ageing related changes
        return gnome
    }
}

gnomes = gnomes.map{ $0.aged(140) }

Of course, reference types still have their place in programming, which may well be a better fit in this case. The friction we are experiencing here suggests that we are trying to treat these structures as if they were objects. If that is the behaviour you need, then you should consider implementing Gnome as a class.

like image 23
Milos Avatar answered Sep 18 '22 14:09

Milos