【问题标题】:Calling a function in swift correctly正确快速调用函数
【发布时间】:2016-08-13 14:26:03
【问题描述】:

我正在开发一个 iPhone 应用程序并且有一个小问题。 我有一个函数,想在我的 viewDidLoad 函数中调用这个函数。我已经尝试过这种方式,但是当我想调用 dropPin 函数时出现错误。

override func viewDidLoad() {
    super.viewDidLoad()

    //drop the pin when view did load !Missing argument for parameter ¨didUpdateLocation¨ in call!
    dropPin(CLLocationManager, didUpdateLocations: [CLLocation])
}

func dropPin(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    let location = locations.last
    let center = CLLocationCoordinate2D (latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)

    //initialize our Pin with our coordinates and the context from AppDelegate
    let pin = Pin(annotationLatitude: center.latitude, annotationLongitude: center.longitude, context: appDelegate.managedObjectContext!)
    //add the annotation to the map
    mapView.addAnnotation(pin)
    //save our context. We can do this at any point but it seems like a good idea to do it here.
    appDelegate.saveContext()
}

感谢您的帮助!

【问题讨论】:

  • 该错误作为注释行发布在问题中代码块的第四行。 //drop the pin when view did load !Missing argument for parameter ¨didUpdateLocation¨ in call!
  • 您没有将对象的实例传递给您的函数,您正在传递类的名称。在深入了解之前,您最好先阅读一本非常基础的编程书籍。找出类的定义和类的实例(即对象)之间的区别是什么。如果有人发布了正确的代码并且你复制它并且它可以工作,那一切都很好,但是你需要了解你做错了什么,这样做的一个好方法是花几个小时阅读一本基本的 Swift 书籍,或者类似的教材,适合初学者。
  • 而且不只是第一个参数不正确。
  • 是的@ZGski 是对的。对不起,当这不清楚时。你是对的。我是 SWIFT 的初学者。但我认为最好的学习方式是“边做边学”。因此,如果您能分享一些代码,我将不胜感激。
  • @SausageMachine 好的,我会看一些书。感谢您的宝贵时间。

标签: ios swift function swift2 cllocationmanager


【解决方案1】:
[CLLocation] 

是一个未实例化的 CLLocations 数组。它还不是一个数组。如果你这样做了

[CCLocation]()

这将是一个包含 0 个对象的实例化数组。如果你想传递一个空白数组,那就太酷了。但是后来

let location = locations.last 

会出现问题,因为没有最后一个对象。它是空的。

【讨论】:

    【解决方案2】:

    您的错误是您传递了类型(CLLocationManager[CLLocation])而不是这些类型的实例

    【讨论】:

      【解决方案3】:

      我建议您重新命名您的问题,这样的标题您不会得到您寻求的帮助,您的问题与核心位置有关...

      也就是说,你的错误实际上是在告诉你出了什么问题......

      !调用中的参数“didUpdateLocation”缺少参数!

      意思是,didUpdateLocation 的值不存在....

      想一想...您还没有显示所有代码,但我敢打赌您还没有询问定位服务它的当前位置...

      例如....

      确保你的类定义正确...

      class myClassName: UINavigationControllerDelegate, CLLocationManagerDelegate {

      并且您在某个地方有一个 var 是您的位置管理器 var dropPin: CLLocationManager!

      正如你所做的那样,实现你的功能...... func dropPin(manager: CLLocationManager, didUpdateLocations 位置: [CLLocation]) {

      那么你需要确保你有类似的东西 self.dropPin = CLLocationManager() self.dropPin.delegate = 自我 self.dropPin.desiredAccuracy = kCLLocationAccuracyBest checkLocationAuthorizationStatus()

      在您的 checlLocationAuthorisationStatus 函数中,您需要 检查您的位置服务是否已启用... CLLocationManager.locationServicesEnabled()

      哦,伙计,这将是一个很长的答案 - 但没有你所有的代码......无论如何,我认为你应该重命名你的问题并显示更多代码......定位服务不是 20 秒的事情实施…… 您将需要 dropPin.requestWhenInUseAuthorization() 和一堆其他部分,最终您将... dropPin.startUpdatingLocation() 然后你就可以真正开始使用了

      祝你好运……

      【讨论】:

      • 感谢您的回答。我已经完成了所有位置服务的实现,并且运行良好。我真的认为我刚刚将函数称为 false。但无论如何。我直接在我的 vieDidLoad 中实现了我的函数,因为我只需要这个函数一次。这对我有用。但我想知道未来我的失败是什么。
      猜你喜欢
      • 2020-06-05
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 2018-05-15
      • 2019-11-30
      • 2019-10-31
      • 1970-01-01
      相关资源
      最近更新 更多