0

I'm working on a iPhone App and have a little question. I have a function and want to call this function in my viewDidLoad-function. I have tried this way but I get an error when I want to call the dropPin-function.

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()
}

Thanks for your help!

M.Wordman
  • 1
  • 2
  • The error is posted as a commented line on the fourth line of the code block within the question. `//drop the pin when view did load !Missing argument for parameter ¨didUpdateLocation¨ in call!` – ZGski Apr 20 '16 at 18:23
  • 5
    You are not passing instances of objects to your function you are passing the names of classes. It would be a good idea for you to read a very basic programming book before going too much further. Find out what the difference between a definition of a class and an instance of a class (i.e. an object) is. Its all very well if somebody posts the correct code and you copy it and it works, but you need to understand what you have done wrong, and a good way of doing that is to spend a couple of hours reading a basic Swift book, or similar teaching material, which is intended for absolute beginners. – Gruntcakes Apr 20 '16 at 18:27
  • And its not just the first parameter which is incorrect. – Gruntcakes Apr 20 '16 at 18:31
  • yes @ZGski is right. Sorry when this what not clear. And you are right. I am a beginner with SWIFT. But I think the best way to learn is 'learning by doing'. So if you can share some code I would be very grateful. – M.Wordman Apr 20 '16 at 18:32
  • @SausageMachine ok I will go over some books. Thank you for your time. – M.Wordman Apr 20 '16 at 18:59
  • I'm more Obj-C than Swift and am currently on a windows PC so for these two reason don't want to post any code just in case it doesn't compile and you copy/paste it. Have a look for a tutorial on using location manager in swift and you should get lots of code to look at. – Gruntcakes Apr 20 '16 at 19:03
  • @SausageMachine thats okay. You are right... I need to understand what the problem is. – M.Wordman Apr 20 '16 at 19:06

3 Answers3

0
[CLLocation] 

is an uninstantiated array of CLLocations. It's not an array yet. If you did

[CCLocation]()

It would be an instantiated array with 0 objects. Thats cool if you want to pass a blank array. But then

let location = locations.last 

will have a problem because there is no last object. Its empty.

Andrew McKinley
  • 1,137
  • 1
  • 6
  • 11
0

Your mistake is that you pass types (CLLocationManager, [CLLocation]) instead of instances of these types.

Alexander Doloz
  • 4,078
  • 1
  • 20
  • 36
0

I would recommend re-titleing your question, you will not get the help you seek with a title like that, your question relates to Core Location...

That said, your error is actually telling you what is wrong...

!Missing argument for parameter ¨didUpdateLocation¨ in call!

meaning, the value of didUpdateLocation isn't there....

Think about it... you haven't shown all your code, but I am willing to bet that you haven't asked location services what its current location is yet...

for example....

make sure things are right up at your class definition...

class myClassName: UINavigationControllerDelegate, CLLocationManagerDelegate {

and that you have a var that is your location manager someplace var dropPin: CLLocationManager!

and as you have done, implement your function... func dropPin(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

then you need to make sure you have something like self.dropPin = CLLocationManager() self.dropPin.delegate = self self.dropPin.desiredAccuracy = kCLLocationAccuracyBest checkLocationAuthorizationStatus()

and in your checlLocationAuthorisationStatus function you will need to check if your location services are enabled... CLLocationManager.locationServicesEnabled()

O, man, this is going to be a long answer - but without all your code... anyway, I think you should rename your question and show more code.... location services isn't a 20 second thing to implement.... you will need dropPin.requestWhenInUseAuthorization() and a bunch of other parts, eventually you will... dropPin.startUpdatingLocation() and then you can actually start using

good luck...

Ash
  • 1
  • 1
  • Thank you for your answer. I already got all the location services implementation and it works fine. I really think that I have just called the function false. But anyway. I implemented my function directly to my vieDidLoad, because I just need this function once. This works for me. But I want to know for the future what my failure was. – M.Wordman Apr 21 '16 at 11:41