'self' captured by a closure before all members were initialized

前端 未结 3 2099
囚心锁ツ
囚心锁ツ 2020-12-11 01:02

Alright, so just to start off, heres my code:

import UIKit
import ForecastIO

class Weather {
    var temp: Float
    var condition: String
    var wind: Fl         


        
3条回答
  •  误落风尘
    2020-12-11 01:38

    I'd hazard to guess that your are running into a concurrency problem. You are probably trying to access your object's properties before the asynchronous call to the DarkSkyClient returns (my apologies in advance if I got this wrong). i.e., the order of events is...

    1. Weather object is initialized, setting temp to 0
    2. Call to DarkSkyClient begins, runs in the background
    3. Read temp variable - hey, it's 0!
    4. Call to DarkSkyClient completes, sets the temp to the value you really wanted. Bad

    So what you really need to do is switch to an inversion of control pattern:

    class Weather {
        var temp: Float
        var condition: String
        var wind: Float
        var precip: Float
    
        init(forecast: Forecast) {
            temp = (forecast.currently?.temperature)!
            condition = (forecast.currently?.summary)!
            wind = (forecast.currently?.windSpeed)!
            precip = (forecast.currently?.precipitationProbability)!
        }
    
        static func getWeather() {
            DarkSkyClient(apiKey: "").getForecast(latitude: Utils().getLat(), longitude: Utils().getLong()) { result in
    
                switch result {
                case .success(let currentForecast, _):
                    let weather = Weather(forecast: currentForecast)
                    // Display the weather somewhere
                    doSomethingWith(weather: weather)
                case .failure(let error):
                    print(error)
                }
            }
        }    
    }
    

    If you're not familiar with developing with asynchronous APIs it's worth your while to read up on the subject; it can be very tricky (sadly, I don't have any recommendations for a good primer). Hope this helps!

提交回复
热议问题