ios - Swift cannot assign immutable value of type [CLLocationCoordinate2D] -


can explain why receive error "cannot assign immutable value of type [cllocationcoordinate2d]" give 2 scenarios. reason want second 1 work because in loop , need pass 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 not work:

override func viewdidload() {     super.viewdidload()      // 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 error shows     mapview.addannotation(shape) } 

i not understand why not work. have println(coordinates) vs println(coords) , gives me same output each.

when passing parameters function, passed immutable default. same if declared them let.

when pass coords param mgpolygon method, it's passed inout parameter, means values can change, because parameter immutable value default, compiler complains.

you can fix explicitly telling compiler parameter can modified prefixing var.

func drawshape(var coords: [cllocationcoordinate2d]) {     var shape = mglpolygon(coordinates: &coords, count: uint(coords.count))      mapview.addannotation(shape) } 

prefixing parameter var means can mutate value within function.

edit: swift 2.2

use keyword inout instead.

func drawshape(inout coords: [cllocationcoordinate2d]) {     var shape = mglpolygon(coordinates: &coords, count: uint(coords.count))      mapview.addannotation(shape) } 

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -