I would like to know if there is a more elegant way to write this:
struct S { var state: [String: Any] public var amounts: [Amount] { var result: [Amount] = [] (self.state["amounts"] as? [Any]?)??.forEach({ a in result.append(Amount(a)) }) return result } } struct Amount { init(_ any: Any?) {} } I have tried using map for array, but I can't find a way to do so.
amountsis most likely something more specific than[Any]amountsarray would be filled based on optional value fromstate, I'd suggest to declare it as an optional array ([Amount]?).