thinkgeekguy thinkgeekguy - 4 months ago 16
Swift Question

Swift cannot assign immutable value of type [CLLocationCoordinate2D]

Can someone explain why I receive the error "cannot assign immutable value of type [CLLocationCoordinate2D]" I will give two scenarios. The reason I want the second one to work is because I would be in a loop and need to pass that to the drawShape func each 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 have even
println(coordinates)
vs
println(coords)
and it gives me the same output for each.

Answer

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

When you pass the coords param into the MGPolygon method, it's passed as an inout parameter, which means those values can change, but because the parameter is an immutable value by default, the compiler complains.

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

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

Prefixing a parameter with var means that you can mutate that value within the function.

Edit: Swift 2.2

Use the keyword inout instead.

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