Swift cannot assign immutable value of type [CLLocationCoordinate2D]

12,388

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)
}
Share:
12,388

Related videos on Youtube

thinkgeekguy
Author by

thinkgeekguy

Updated on September 15, 2022

Comments

  • thinkgeekguy
    thinkgeekguy over 1 year

    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.

  • eimmer
    eimmer almost 8 years
    FYI - The compiler warns that the 'var' keyword will be removed in Swift3. I haven't found a good solution beyond copying each field directly. :(
  • EFC
    EFC almost 8 years
    'var' has been replaced by 'inout' in Swift 2.2
  • OOPer
    OOPer almost 8 years
    var parameter is REMOVED. Not "replaced by inout". They have other functionalities. Replacing var to inout can be a workaround only when some conditions are met. In this case, it will work (other than needing & when calling), but in other cases it may not.