“无法分配给值:'i'是'let'常量".错误 [英] "Cannot assign to value: 'i' is a 'let' constant" error

查看:118
本文介绍了“无法分配给值:'i'是'let'常量".错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将项目更新为Swift3.大多数情况都很好,但是我无法弄清楚下面的内容.我相信我已经找到了纠正两个错误中第一个的方法,但是由于我遇到了错误无法分配给值:'i'是'let'常量,因此无法找出第二个错误" i = i + 1" "

I am updating my project to Swift 3. Most all has gone well, but I cannot figure the below out. I believe I have found how to correct the first of the two errors, but cannot figure out the second one "i = i + 1" as I am getting the error "Cannot assign to value: 'i' is a 'let' constant"

我雇了一个人来处理我的应用程序上的坐标,因此这就是为什么我不习惯这些类型的错误.

I hired someone to do the coordinates on my app, so this is why I am not so accustomed to these types of errors.

我已经创建了200个多边形区域,下面是要获取用户当前位置并确定他们所位于的区域并使用该区域中的数据的方法.

I have created 200 polygon regions and the below this is meant to take the users current location and determine with region they are located in and use the data from that region.

我看到了这个问题,但是看不到它如何回答我的问题. 无法分配给值:"i"是一个快速让'let'保持不变

I saw this question, but cannot see how this answers my question. Cannot assign to value: 'i' is a 'let' constant in swift

func format_subRegion_Array_Elements_To_sub_Elements(_ one_element_in_subRegionArray : String) -> [CLLocationCoordinate2D]

{

var boundary: [CLLocationCoordinate2D]

var Boundary_points_in_string_format : [String]
    var Array_of_one_element_in_subRegionArray : [String] = one_element_in_subRegionArray.components(separatedBy: ",")


Boundary_points_in_string_format = []

//  ORIGINAL statement
//  for var i = 0 ; i <= Array_of_one_element_in_subRegionArray.count-1 ; i += 1

//  UPDATED statement
    for (i, sender) in Array_of_one_element_in_subRegionArray.enumerated()

    {
        Boundary_points_in_string_format += [String(Array_of_one_element_in_subRegionArray[i]+","+Array_of_one_element_in_subRegionArray[i+1])]


//  Still get "Cannot assign to value: 'i' is a 'let' constant" error here
        i = i+1

    }

  let  boundaryPointsCount = Boundary_points_in_string_format.count

boundary = []

for i in 0...boundaryPointsCount-1
{

    let newArrayElement  = Boundary_points_in_string_format[i].components(separatedBy: ",")

    let myDouble1 = Double(newArrayElement[0])
    let myDouble2 = Double(newArrayElement[1])
    boundary += [CLLocationCoordinate2DMake(CLLocationDegrees(myDouble1!), CLLocationDegrees(myDouble2!))]

}

return boundary
}

谢谢

已更新,其中包含来自该类的更多数据. @Alexander Momchliov,我已用您的代码更新了错误"区域,但出现一个错误

UPDATED with more data from the class. @Alexander Momchliov, I have updated the "bad" area with your code, but get a single error

let polygon = MKPolygon(coordinates: &boundary, count: boundary.count)

错误是无法将值类型'(String)'-> [CLLocaitonCoordinate2D]'转换为预期的参数类型"CLLocationCoordinate2D'",并指向& boundry

The error is "Cannot convert value type '(String)' -> [CLLocaitonCoordinate2D]' to expected argument type "CLLocationCoordinate2D'" and points to the &boundry

func scanAllGPSData(_ currentLatitude : Double , currentLongitude : Double)->Bool
{
    for i in 0 ... subRegionArray.count-1

    {
        let singleElementInSubRegionArray = subRegionArray[i].coordinates as String

        var boundary = formatSubRegionArray(singleElementInSubRegionArray: )

        let polygon = MKPolygon(coordinates: &boundary, count: boundary.count)

        let polygonRenderer = MKPolygonRenderer(polygon: polygon)

        let currentLocationCoordinate : CLLocationCoordinate2D = CLLocationCoordinate2DMake( currentLatitude,currentLongitude)

        let currentMapPoint: MKMapPoint = MKMapPointForCoordinate(currentLocationCoordinate)
        let polygonViewPoint: CGPoint = polygonRenderer.point(for: currentMapPoint)

//                if CGPathContainsPoint(polygonRenderer.path, nil, polygonViewPoint, true)

            if polygonRenderer.path.contains(polygonViewPoint)

            {
                print("Your are INSDIE Subregion Species Area.")

                subregion.PolygonInMyCurrentLocation = polygon

            AppDelegate.getAppState().filterByRegionID = String(subRegionArray[i].id)+",subregion_code"
            AppDelegate.getAppState().save()
            AppDelegate.getAppState().IsSpeciesFoundInSubRegionPolygonArea = true
            AppDelegate.getAppState().save()
                return true
            }

            else

            {
            }
    }


    print("Your are out of Subregion Species Area.")
    subregion.PolygonInMyCurrentLocation = nil

    AppDelegate.getAppState().filterByRegionID = ""
    AppDelegate.getAppState().save()
    AppDelegate.getAppState().IsSpeciesFoundInSubRegionPolygonArea = false
    AppDelegate.getAppState().save()

return false
}



func formatSubRegionArray(singleElementInSubRegionArray: String) -> [CLLocationCoordinate2D]
{
    var subRegionComponents = singleElementInSubRegionArray.components(separatedBy: ",")
    var boundary = [CLLocationCoordinate2D]()

    for i in stride(from: 0, to: subRegionComponents.count, by: 2) {
        let first = subRegionComponents[i]
        let second = subRegionComponents[i + 1]
        boundary.append(CLLocationCoordinate2D(latitude: CLLocationDegrees(first)!, longitude: CLLocationDegrees(second)!))
    }

    return boundary
}


/*
 func format_subRegion_Array_Elements_To_sub_Elements(one_element_in_subRegionArray : String) -> [CLLocationCoordinate2D]

 {

 var boundary: [CLLocationCoordinate2D]

 var Boundary_points_in_string_format : [String]
 let Array_of_one_element_in_subRegionArray : [String] = one_element_in_subRegionArray.componentsSeparatedByString(",")


 Boundary_points_in_string_format = []

 for var i = 0 ; i <= Array_of_one_element_in_subRegionArray.count-1 ; i += 1

 {
 Boundary_points_in_string_format += [String(Array_of_one_element_in_subRegionArray[i]+","+Array_of_one_element_in_subRegionArray[i+1])]

 i = i+1

 }

 let  boundaryPointsCount = Boundary_points_in_string_format.count

 boundary = []

 for i in 0...boundaryPointsCount-1
 {

 let newArrayElement  = Boundary_points_in_string_format[i].componentsSeparatedByString(",")

 let myDouble1 = Double(newArrayElement[0])
 let myDouble2 = Double(newArrayElement[1])
 boundary += [CLLocationCoordinate2DMake(CLLocationDegrees(myDouble1!), CLLocationDegrees(myDouble2!))]

 }

 return boundary
}
*/

推荐答案

此代码不必要地复杂.令人震惊的是,实际上.这让我感到非常困惑.这是我重新创建它的最佳尝试:

This code is needlessly complex. Shockingly so, actually. I'm quite impressed how obfuscated this is. Here's my best attempt at recreating it:

func format(subRegion: String) -> [CLLocationCoordinate2D] {
    var subRegionComponents = subRegion.components(separatedBy: ",")

    let boundaries = stride(from: 0, to: subRegionComponents.count - 1, by: 2).map { i in
        let first = subRegionComponents[i], let second = subRegionComponents[i + 1]
        return CLLocationCoordinate(CLLocationDegrees(first)!, CLLocationDegrees(second)!)
    }

    return boundaries
}

  1. Swift的约定是将LowerCamelCase用于变量名,将CapitalCamelCase用于类型.请坚持下去.
  2. 如果不需要,请不要分开进行赋值和定义.只需使用var boundary = [CLLocationCoordinate2D]()
  3. ,而不是var boundary: [CLLocationCoordinate2D]boundary = []
  4. 不要添加多余的类型注释,除非它们实际上增加了清晰度,而不是降低清晰度.
  5. 不要明确地将事物命名为"...的数组".只是多元化.其类型将是隐式的.
  6. 使用开放范围运算符(...)时不要手动减一.只需使用封闭范围运算符(..<).例如0..<boundaryPointsCount代替0...boundaryPointsCount-1
  7. 只要可以使用array.indices
  8. ,就不要使用0..<array.count
  1. Swift's convention is to use lowerCamelCase for variable names, and CapitalCamelCase for types. Stick to it, please.
  2. Don't split a delcaration and defintion apart if you don't have to. Rather than var boundary: [CLLocationCoordinate2D] and boundary = [], just use var boundary = [CLLocationCoordinate2D]()
  3. Don't add redundant type annotations unless they actually add clarity rather than detract from it.
  4. Don't explicitly name things "array of ...". Just pluralize. Its type will be implicit.
  5. Don't manually subtract one when using the open range operator (...). Just use the closed range operator (..<). e.g. 0..<boundaryPointsCount instead of 0...boundaryPointsCount-1
  6. Don't use 0..<array.count when you can just use array.indices

这篇关于“无法分配给值:'i'是'let'常量".错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆