How to change the value of the structure that is in the array?

I use quick for my project.

I have an array of structures with a name Instrument. Later I created a function that returns a specific one Instrumentfrom an array. Then I wanted to change the value to one of its properties, but this change is not reflected inside the array.

I need this array to include all changes inside the elements. What do you think is best here?

  • Change Instrumentfrom structto class.
  • Somehow rewrite a function that returns Instrumentfrom an array.

I am using this function now:

func instrument(for identifier: String) -> Instrument? {
  if let instrument = instruments.filter({ $0.identifier == identifier }).first {
    return instrument
  }
  return nil
}

I start with structure because swift is known as a language for structures, and I want to know when to use it struct class.

thank

+4
2

struct Instrument .

struct Instrument {
    let identifier: String
    var value: Int
}

var instruments = [
    Instrument(identifier: "alpha", value: 3),
    Instrument(identifier: "beta", value: 9),
]

if let index = instruments.index(where: { $0.identifier == "alpha" }) {
    instruments[index].value *= 2
}

print(instruments) // [Instrument(identifier: "alpha", value: 6), Instrument(identifier: "beta", value: 9)]
+8

( , identifier : ), , [Instruments], () Instrument , . . ( @Hamish !):

struct Instrument {
    let identifier: String
    var changeThis: Int
    init(_ identifier: String, _ changeThis: Int) {
        self.identifier = identifier
        self.changeThis = changeThis
    }
}

struct Foo {
    var instruments: [Instrument]

    @discardableResult // do not necessarily make use of the return result (no warning if not)
    mutating func updateInstrument(forFirst identifier: String,
            using mutate: (inout Instrument) -> ()) -> Bool {
        if let idx = instruments.indices
            .first(where: { instruments[$0].identifier == identifier }) {

            // mutate this instrument (in-place) using supplied closure
            mutate(&instruments[idx])

            return true // replacement successful
        }
        return false // didn't find such an instrument
    }
}

:

var foo = Foo(instruments:
    [Instrument("a", 1), Instrument("b", 2),
     Instrument("c", 3), Instrument("b", 4)])

// make use of result of call
if foo.updateInstrument(forFirst: "b", using: { $0.changeThis = 42 }) {
    print("Successfully mutated an instrument")
} // Successfully mutated an instrument

// just attempt mutate and discard the result
foo.updateInstrument(forFirst: "c", using: { $0.changeThis = 99 })

print(foo.instruments)
/* [Instrument(identifier: "a", changeThis: 1), 
    Instrument(identifier: "b", changeThis: 42), 
    Instrument(identifier: "c", changeThis: 99),
    Instrument(identifier: "b", changeThis: 4)] */

@Owen: s answer, index(where:) ( indices.first(where:), ). index(where:)

if let idx = instruments.indices
    .first(where: { instruments[$0].identifier == identifier }) { ...

if let idx = instruments
    .index(where: { $0.identifier == identifier }) { ...

updateInstrument(forFirst:using) Foo.

updateInstrument(forFirst:using), map Optional () :

struct Foo {
    var instruments: [Instrument]

    @discardableResult
    mutating func updateInstrument(forFirst identifier: String,
        using mutate: (inout Instrument) -> ()) -> Bool {
        return instruments
            .index(where: { $0.identifier == identifier })
            .map { mutate(&instruments[$0]) } != nil
    }
}
+1

Source: https://habr.com/ru/post/1666854/


All Articles