[英]Swift Closure, Missing argument for parameter #1 in call
Hello all who want to help. 大家好! I am trying to do a callback function when I get my location updated. 更新位置时,我正在尝试做一个回调函数。 But I run into a problem that I have no idea how to solve. 但是我遇到了一个我不知道如何解决的问题。
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.
}
} }
The error occurs here when I call loadData() 当我调用loadData()时在这里发生错误
let location = LocationService(){
callBackFunction in
println("data is loading")
loadData()
}
Here is the LocationService class 这是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()
}
} }
From what I have found myself it is something about calling loadData() as a class function while it is a instance function. 从我发现自己的情况来看,这是关于将loadData()作为类函数而作为实例函数调用的。 Im still very new to swift so I don't really know how to solve the problem, so any help would be greatly appreciated. 我还是很快就很陌生,所以我真的不知道如何解决该问题,因此我们将不胜感激。
Edit: I found the solotion. 编辑:我找到了这样。 The reason that the error was popping op was because the function loadData() was initilaized but the callBackFunction wasn't properly initilaized. 该错误弹出op的原因是因为函数loadData()已初始化,但是callBackFunction没有正确初始化。 I solved it by moving the callBackFunction to getLocation() instead of the initialization. 我通过将callBackFunction移到getLocation()而不是初始化的方法来解决它。
The issue is with the way you are passing the callback, instead of: 问题在于传递回调的方式,而不是:
let location = LocationService(){
callBackFunction in
loadData()
}
You need to pass the callback like: 您需要像这样传递回调:
let location = LocationService(callBackFunc:loadData)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.