How can I make the default value to nil? I've tried to make the StockInfo optional in the func stockInfoPerDay(jsonData: StockInfo?) and func stockInfoSingleDay(singleJSONData: StockInfoOneDay?) but this leaves me with trouble in the for-loop.... It shows no errors when Force-unwrapping but I would like to not force-unwrap.
.onAppear(){ stockInfoPerDay(jsonData: jsonData ?? nil) <---- *'nil' is not compatible with expected argument type 'StockInfo'* stockInfoSingleDay(singleJSONData: singleJSONData ?? nil) <---- *'nil' is not compatible with expected argument type 'StockInfoOneDay'* } static var pricesForOneMonth: [CGFloat] = [] static var priceForOneDay: [StockInfoOneDay] = [] func stockInfoPerDay(jsonData: StockInfo) { CropPriceView.pricesForOneMonth.removeAll() for data in jsonData.data{ CropPriceView.pricesForOneMonth.append(CGFloat(data.close)) } } func stockInfoSingleDay(singleJSONData: StockInfoOneDay){ CropPriceView.priceForOneDay.removeAll() CropPriceView.priceForOneDay.append(singleJSONData) }
guardstatement. So decide what should happen when it is nil and write code to handle that situation.