[英]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.