簡體   English   中英

Swift閉包,調用中參數#1的參數丟失

[英]Swift Closure, Missing argument for parameter #1 in call

大家好! 更新位置時,我正在嘗試做一個回調函數。 但是我遇到了一個我不知道如何解決的問題。

class ViewController: UIViewController {

@IBOutlet weak var currentTemperatureLabel: UILabel?
@IBOutlet weak var currentHumidityLabel: UILabel?
@IBOutlet weak var currentPrecipitationLabel: UILabel?
@IBOutlet weak var currentWeatherIcon: UIImageView?
@IBOutlet weak var currentWeatherSummary: UILabel?

private let APIKey = "someString"

let location = LocationService(){
    callBackFunction in
    loadData()
}

func loadData()
{
    let forecastService = ForecastService(APIKey: APIKey)
    forecastService.getForecast(location.latitude, long: location.longitude)
        {
            (let currentW) in
            if let currentWeather = currentW
            {
                dispatch_async(dispatch_get_main_queue())
                    {

                        if tjekIfNil(currentWeather.temperature)
                        {
                            self.currentTemperatureLabel?.text = "\(currentWeather.temperature!)°"
                        }
                        if tjekIfNil(currentWeather.humidity)
                        {
                            self.currentHumidityLabel?.text = "\(currentWeather.humidity!)%"
                        }
                        if tjekIfNil(currentWeather.precipProbability)
                        {
                            self.currentPrecipitationLabel?.text = "\(currentWeather.precipProbability!)%"
                        }
                        if tjekIfNil(currentWeather.icon)
                        {
                            self.currentWeatherIcon?.image = currentWeather.icon
                        }
                        if tjekIfNil(currentWeather.summary)
                        {
                            self.currentWeatherSummary?.text = currentWeather.summary
                        }
                }
            }
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
        location.requestPermission()
        location.getLocation()
    }

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

當我調用loadData()時在這里發生錯誤

let location = LocationService(){
    callBackFunction in
    println("data is loading")
    loadData()
}

這是LocationService類

import CoreLocation
class LocationService: NSObject, CLLocationManagerDelegate
{
var locationManager : CLLocationManager!
var location : CLLocation?
var longitude : Double?
var latitude : Double?
let callBackFunction : ()->()

init(callBackFunc: (Void->Void))
{
    locationManager = CLLocationManager()
    callBackFunction = callBackFunc
}

func requestPermission()
{
    locationManager.requestWhenInUseAuthorization()
}
func getLocation()
{
    if(CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedWhenInUse)
    {
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.startUpdatingLocation()

    }else
    {
        location = nil
        longitude = nil
        latitude = nil
    }

}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!)
{
    location = locationManager.location
    if tjekIfNil(location?.coordinate.longitude)
    {longitude = location!.coordinate.longitude}
    if tjekIfNil(location?.coordinate.latitude)
    {latitude = location!.coordinate.latitude}
    locationManager.stopUpdatingLocation()
    println(longitude)
    println(latitude)
    callBackFunction()
}

}

從我發現自己的情況來看,這是關於將loadData()作為類函數而作為實例函數調用的。 我還是很快就很陌生,所以我真的不知道如何解決該問題,因此我們將不勝感激。

編輯:我找到了這樣。 該錯誤彈出op的原因是因為函數loadData()已初始化,但是callBackFunction沒有正確初始化。 我通過將callBackFunction移到getLocation()而不是初始化的方法來解決它。

問題在於傳遞回調的方式,而不是:

let location = LocationService(){
    callBackFunction in
    loadData()
}

您需要像這樣傳遞回調:

let location = LocationService(callBackFunc:loadData)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM