【问题标题】:Missing argument for parameter 'completionHandler' in call调用中缺少参数“completionHandler”的参数
【发布时间】:2015-07-23 14:34:34
【问题描述】:

我使用了在 swift 1.0 中工作的函数 geocodeAddressString,但在 swift2 中没有。谁能告诉我我的代码有什么问题以及如何解决这个问题?谢谢!

geocoder.geocodeAddressString(address, {(placemarks: [AnyObject], error: NSError) -> Void in  //Error: Missing argument for parameter 'completionHandler' in call

        if let placemark = placemarks?[0] as? CLPlacemark {
            let annotation = MKPointAnnotation()
            let location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(placemark.location.coordinate.latitude, placemark.location.coordinate.longitude)
            annotation.coordinate = location
            annotation.title = "\(StudentArray[student].firstName), \(StudentArray[student].lastName)"
            annotation.subtitle = "\(StudentArray[student].grade)"
            self.mapView.addAnnotation(annotation)
        }

    })

【问题讨论】:

    标签: swift completionhandler completion


    【解决方案1】:

    要么指定completionHandler参数名称:

    geocoder.geocodeAddressString(address, completionHandler: { placemarks, error in
        if let placemark = placemarks.first as? CLPlacemark {
            let annotation = MKPointAnnotation()
            let location = CLLocationCoordinate2DMake(placemark.location.coordinate.latitude, placemark.location.coordinate.longitude)
            annotation.coordinate = location
            annotation.title = "\(StudentArray[student].firstName), \(StudentArray[student].lastName)"
            annotation.subtitle = "\(StudentArray[student].grade)"
            self.mapView.addAnnotation(annotation)
        }
    })
    

    或使用尾随闭包语法(参见 Trailing Closure 部分 ClosuresSwift 编程语言 章节),您可以从 @987654324 中拉出闭包@和)放在)之后:

    geocoder.geocodeAddressString(address) { placemarks, error in
        if let placemark = placemarks.first as? CLPlacemark {
            let annotation = MKPointAnnotation()
            let location = CLLocationCoordinate2DMake(placemark.location.coordinate.latitude, placemark.location.coordinate.longitude)
            annotation.coordinate = location
            annotation.title = "\(StudentArray[student].firstName), \(StudentArray[student].lastName)"
            annotation.subtitle = "\(StudentArray[student].grade)"
            self.mapView.addAnnotation(annotation)
        }
    }
    

    【讨论】:

    • 感谢您提供此示例!尾随闭包语法总是让我感到困惑,因为它省略了部分参数,但现在我发现它只是简单的练习。
    【解决方案2】:

    在完成处理程序之前添加参数。

    geocoder.geocodeAddressString(address, completionHandler: {(placemarks: [AnyObject], error: NSError) -> Void in  // Added argument for parameter 'completionHandler' in call
    
        if let placemark = placemarks?[0] as? CLPlacemark {
            let annotation = MKPointAnnotation()
            let location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(placemark.location.coordinate.latitude, placemark.location.coordinate.longitude)
            annotation.coordinate = location
            annotation.title = "\(StudentArray[student].firstName), \(StudentArray[student].lastName)"
            annotation.subtitle = "\(StudentArray[student].grade)"
            self.mapView.addAnnotation(annotation)
        }
    
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-01
      • 2017-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多