Swift cannot assign immutable value of type [CLLocationCoordinate2D]

Can someone explain why I get an error message? I can’t assign an immutable value like [CLLocationCoordinate2D] "I will give two scripts. The reason I want the second to work is because I will be in a loop and have to pass this to drawShape every time.

This code works:

func drawShape() { var coordinates = [ CLLocationCoordinate2D(latitude: 40.96156150486786, longitude: -100.24319656647276), CLLocationCoordinate2D(latitude: 40.96456685906742, longitude: -100.25021235388704), CLLocationCoordinate2D(latitude: 40.96528813790064, longitude: -100.25022315443493), CLLocationCoordinate2D(latitude: 40.96570116316434, longitude: -100.24954721762333), CLLocationCoordinate2D(latitude: 40.96553915028926, longitude: -100.24721925915219), CLLocationCoordinate2D(latitude: 40.96540144388564, longitude: -100.24319644831121), CLLocationCoordinate2D(latitude: 40.96156150486786, longitude: -100.24319656647276), ] var shape = MGLPolygon(coordinates: &coordinates, count: UInt(coordinates.count)) mapView.addAnnotation(shape) } 

This code does NOT work:

 override func viewDidLoad() { super.viewDidLoad() // does stuff var coords: [CLLocationCoordinate2D] = [ CLLocationCoordinate2D(latitude: 40.96156150486786, longitude: -100.24319656647276), CLLocationCoordinate2D(latitude: 40.96456685906742, longitude: -100.25021235388704), CLLocationCoordinate2D(latitude: 40.96528813790064, longitude: -100.25022315443493), CLLocationCoordinate2D(latitude: 40.96570116316434, longitude: -100.24954721762333), CLLocationCoordinate2D(latitude: 40.96553915028926, longitude: -100.24721925915219), CLLocationCoordinate2D(latitude: 40.96540144388564, longitude: -100.24319644831121), CLLocationCoordinate2D(latitude: 40.96156150486786, longitude: -100.24319656647276), ] self.drawShape(coords) } func drawShape(coords: [CLLocationCoordinate2D]) { var shape = MGLPolygon(coordinates: &coords, count: UInt(coords.count)) //---this is where the error shows up mapView.addAnnotation(shape) } 

I do not understand why this does not work. I even have println(coordinates) vs println(coords) and it gives me the same result for each.

+6
source share
1 answer

When passing parameters to a function, they are passed as immutable by default. Just as if you declared them as let .

When you pass the coords parameter to the coords method, it is passed as the inout , which means that these values ​​can change, but since the default parameter is an immutable value, the compiler complains.

You can fix this by explicitly telling the compiler that this parameter can be changed by prefixing it with var .

 func drawShape(var coords: [CLLocationCoordinate2D]) { var shape = MGLPolygon(coordinates: &coords, count: UInt(coords.count)) mapView.addAnnotation(shape) } 

The var parameter prefix means that you can change this value inside the function.

Edit: Swift 2.2

Use the inout keyword instead.

 func drawShape(inout coords: [CLLocationCoordinate2D]) { var shape = MGLPolygon(coordinates: &coords, count: UInt(coords.count)) mapView.addAnnotation(shape) } 
+14
source

All Articles