I want to take say a stock price and then generate values within .05% in a 2D array.
I am passing the stock information such as stock name and stock price.
But when I try and run a method that creates the 2D array of values I am met with a "Cannot use instance member '' within property initializer; property initializers run before 'self' is available"
I was looking how to get around this.
Say Amazon stock is 2400. This data passes into the new view. What I want to do is create a 2D array of values near 2400, perhaps 2405 and 2395. I want a 2D array of these values generated in the new view. But I am getting an error that I cannot generate the values until the view is loaded, but I need them to generate the view.
I think I have to use init method but I am not sure how.
This is the line that is throwing the error above.
var new_prices = generateValues(price: position.stock_price)
Here is the method
func generateValues(price: Double) -> [[Double]] {
var values = [[Double]]()
for x in 1...5 {
values[0][x-1] = (price + price * 0.005 * Double(x))
}
return values
}
This is what is being passed in the stock postion
struct Position : Identifiable {
var id = UUID()
var stock_name: String
var stock_ticker: String
var stock_price: Double
var price_paid: Double
}
Arguments passed throwing an error for no argument init().
struct PositionDetail_Previews: PreviewProvider {
static var previews: some View {
PositionDetail(position: Position(stock_name: "Amazon",
stock_ticker: "AMZN", stock_price : 2400, price_paid : 2300))
}
}
