0

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) } 
1
  • If you want to make an argument optional then you also need to handle the case when it is nil, perhaps with a guard statement. So decide what should happen when it is nil and write code to handle that situation. Commented Feb 9, 2021 at 13:30

2 Answers 2

1

You can't pass nil to a function parameter unless that parameter is an Optional. You need to rewrite your function to accept an optional:

func stockInfoPerDay(jsonData: StockInfo?) { guard let jsonData = jsonData else { return } //Exit if you are passed a nil. CropPriceView.pricesForOneMonth.removeAll() //Move this above the guard if you want to empty the CropPriceView when you are passed a nil. for data in jsonData.data{ CropPriceView.pricesForOneMonth.append(CGFloat(data.close)) } } 

Note that another way you could handle this is to leave your stockInfoPerDay() function alone, and use "optional binding" (if let) to only call your function if the parameter is not nil:

.onAppear(){ if let validJsonData = jsonData { stockInfoPerDay(jsonData: validJsonData) } if let validSingleJSONData = jsonData { stockInfoSingleDay(singleJSONData: validSingleJSONData) } } 
Sign up to request clarification or add additional context in comments.

Comments

0

You need to ideally set it up so that your functions accept optionals as parameters and handle the nil case in the functions, often with a guard statement. However, since you said you had an problem with that and I don't know what that problem is, I can just suggest changing your code to something like this

stockInfoPerDay(jsonData: jsonData ?? StockInfo()) // <- Just make sure you have a good default initializer for the StockInfo object. Handle the possibility that jsonData was nil and StockInfo is empty inside the stockInfoPerDay function. stockInfoSingleDay(singleJSONData: singleJSONData ?? StockInfoOneDay()) 

Let me know if I this helps.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.