Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating an item in an array passed by reference

What's the easiest/right way to update an item in an array? I want the caller to have the updated array as well. So:

static func updateItem(updatedItem: Item, inout items: [Item]) -> Bool {
        var item = items.filter{ $0.id == updatedItem.id }.first
        if item != nil {
            item = updatedItem
            return true
        }

        return false
    }

I want the caller to have the updated items (with the updated item). I think the problem with the above code is that it only updates the local variable item. What's the best way to actually update the relevant item inside the items array?

like image 651
Prabhu Avatar asked Mar 10 '23 07:03

Prabhu


1 Answers

You do it the same way Superman gets into his tights — one leg at a time. Cycle through the incoming inout array and replace any items where the id matches:

func updateItem(updatedItem: Item, items: inout [Item]) -> Bool {
    var result = false
    for ix in items.indices {
        if items[ix].id == updatedItem.id {
            items[ix] = updatedItem
            result = true
        }
    }
    return result
}

Note that this is Swift 3 syntax where inout precedes the type, not the label.

You can write it a little more "Swiftily" by using map:

func updateItem(updatedItem: Item, items: inout [Item]) {
    items = items.map {
        $0.id == updatedItem.id ? updatedItem : $0
    }
}

... but that amounts to the same thing in the end.

like image 200
matt Avatar answered Mar 28 '23 22:03

matt