简体   繁体   English

Swift无法分配类型[CLLocationCoordinate2D]的不可变值

[英]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. 有人可以解释为什么我收到错误“无法分配类型[CLLocationCoordinate2D]的不可变值”我会给出两个场景。 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. 我希望第二个工作的原因是因为我将处于循环中并且需要每次都将它传递给drawShape函数。

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. 我甚至有println(coordinates)println(coords) ,它给了我相同的输出。

When passing parameters to a function, they are passed as immutable by default. 将参数传递给函数时,默认情况下它们将作为不可变传递。 The same as if you declared them as a let . 就像你将它们声明为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. 当您将coords param传递给MGPolygon方法时,它将作为inout参数传递,这意味着这些值可以更改,但由于参数默认为不可变值,因此编译器会抱怨。

You can fix it by explicitly telling the compiler that this parameter can be modified by prefixing it with a var . 您可以通过明确告诉编译器可以通过在其前面加上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. 使用var前缀参数意味着您可以在函数中改变该值。

Edit: Swift 2.2 编辑:Swift 2.2

Use the keyword inout instead. 请改用关键字inout

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我试图掉一个针脚,但出现此错误:无法将类型“ CLLocationManager”的值分配给类型“ CLLocationCoordinate2D” - Im trying to drop a pin but I get this error: Cannot assign value of type 'CLLocationManager' to type 'CLLocationCoordinate2D' 无法将CGPoint分配给CLLocationCoordinate2D - Cannot assign CGPoint to CLLocationCoordinate2D 无法将CLLocationCoordinate2D分配给另一个CLLocationCoordinate2D - Unable to assign a CLLocationCoordinate2D to another CLLocationCoordinate2D 在swift中CLLocation到CLLocationCoordinate2D - CLLocation to CLLocationCoordinate2D in swift 转换UnsafeMutablePointer <CLLocationCoordinate2D> 到Swift中的[CLLocationCoordinate2D] - Convert UnsafeMutablePointer<CLLocationCoordinate2D> to [CLLocationCoordinate2D] in Swift 无法将CLLocationCoordinate2D转换为(CLLocationCoordinate2D) - Cannot convert CLLocationCoordinate2D to (CLLocationCoordinate2D) 将 CLLocationCoordinate2D 放入 CLLocation for Swift - put CLLocationCoordinate2D into CLLocation for Swift 在 Swift 中将 CLLocationCoordinate2D 转换为 CLLocation 时出错 - Error converting CLLocationCoordinate2D to CLLocation in Swift 将CLLocationCoordinate2D数组作为返回类型传递 - Passing CLLocationCoordinate2D Array as return type Iphone CLLocationCoordinate2D - Iphone CLLocationCoordinate2D
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM