I create an object in ContentView of a class and pass it to a function
@StateObject var portfolio = Portfolio()
...
///Cannot pass immutable value as inout argument: 'portfolio' is a get-only property
setCoin(portfolio: &portfolio, currencyList: currencyList, currency: currency, quantity: quantity)
...
func setCoin(portfolio:inout Portfolio, currencyList: [String : Int], currency: String, quantity: String){
for coin in portfolio.coins{
if coin.name == currency{
//Cannot assign to property: 'coin' is a 'let' constant
coin.quantity = Double(quantity)
portfolio.portfolioList.append(coin)
}
}
}
portfolio.coins is an array of objects obtained from a JSON decoding. I am attempting to add a quantity variable that is user input to use in views.
Portfoliois reference type. Andinoutis for value types.Just pass the reference
Edit: To modify
coinswhich is apparently a value type array you have to useenumerated()and modify the item in the array directly by indexI highly recommend to move the last line in the loop - actually the entire logic - into the model