Alright, so just to start off, heres my code:
import UIKit
import ForecastIO
class Weather {
var temp: Float
var condition: String
var wind: Float
var precip: Float
init() {
DarkSkyClient(apiKey: "<api key>").getForecast(latitude: Utils().getLat(), longitude: Utils().getLong()) { result in
switch result {
case .success(let currentForecast, _):
self.temp = (currentForecast.currently?.temperature)!
self.condition = (currentForecast.currently?.summary)!
self.wind = (currentForecast.currently?.windSpeed)!
self.precip = (currentForecast.currently?.precipitationProbability)!
case .failure(let error):
print(error)
}
}
}
}
So my error comes up because I'm trying to initialize temp inside of the API call. I know this isn't the most reliable way of doing it but I'm trying to first get it to work.
The first error is:
'self' captured by a closure before all members were initialized
on the line DarkSkyClient(apiKey: "").getForecast(latitude: Utils().getLat(), longitude: Utils().getLong()) { result in
My second error:
Return from initializer without initializing all stored properties
on the second to last }
Now, obviously I'm not initializing right. I can't find the proper way to do what my end goal is though. Maybe I'm doing this entirely wrong?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…