【发布时间】: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