2016-11-28 21 views
-1

我正在更新我的項目Swift 3.大多數都進展順利,但我不能指出下面。我相信我已經找到了如何糾正兩個錯誤中的第一個,但無法找出第二個「i = i + 1」,因爲我收到錯誤「無法賦值:'i'是'let'常量「「不能賦值:'我'是一個'讓'常量」錯誤

我聘請了某人在我的應用上做座標,所以這就是爲什麼我不習慣這些類型的錯誤。

我已經創建了200個多邊形區域,下面這個是爲了獲取用戶當前的位置並確定它們位於的區域並使用該區域的數據。

我看到這個問題,但看不到這是如何回答我的問題。 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,我已經更新了你的代碼的「壞」的區域,但得到一個錯誤

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

的錯誤是「無法轉換值類型‘(字符串)’ - > [CLLocaitonCoordinate2D]」到期望的參數鍵入「CLLocationCoordinate2D'」,並指向& 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 
} 
*/ 
+0

謝謝,試過了,但得到的指數列於Boundary_points_in_string_format + = [字符串(Array_of_one_element_in_subRegionArray [誤差範圍i] +「,」+ Array_of_one_element_in_subRegionArray [i + 1])] –

+1

請考慮使用Swift命名約定:變量名始終以小寫字母開頭並且是camelCased。這不是JavaScript或PHP。你的代碼很難閱讀。 – vadian

+0

這真的很難看,但我給它一個鏡頭。我會很快回復 – Alexander

回答

2

此代碼不必要的複雜。其實,令人震驚的是。我很困惑這是如何混淆。下面是做做我最好的嘗試:

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]boundary = [],只需使用var boundary = [CLLocationCoordinate2D]()
  3. 不要添加冗餘類型註釋,除非它們實際上增加了清晰度而不是減少它。
  4. 不要明確命名「數組...」。只是複數。它的類型將是隱含的。
  5. 當使用開放式範圍操作符(...)時,請勿手動減去一個。只需使用封閉範圍操作員(..<)。例如0..<boundaryPointsCount代替0...boundaryPointsCount-1
  6. 不要使用0..<array.count時,你可以使用array.indices
+0

感謝您的上述。不知道你是否閱讀過上述內容,但這部分內容是由我不再使用的承包商編寫的。我應該在發佈之前清理命名。我仍然有一些錯誤,但會盡可能清理併發布更多 –

+0

沒問題。我的回答沒有經過測試,所以如果您遇到任何問題,請告訴我。 – Alexander

0

像我看到它,你只是遞增i因爲你從舊系統轉換爲循環式的,在爲與列舉你不需要增加i,因爲你已經擁有的每個項目都是自己的編號。

您無需增加i

由於我是從enumerated()得到的數字。

let strings = ["Dog", "Cat", "Bird"] 

for (i, string) in strings.enumerated() { 
    print(i) 
    print(string) 
} 

輸出:

0 
Dog 
1 
Cat 
2 
Bird 

,如果您使用enumerated()您可以創建一個Int外的對,然後加一。你可以不是改變一個for循環的項目。

1

指數變量定義常量,你不能改變它們。

但是,當你要使用的itemitem + 1在重複循環只需使用stride

typealias Array_of_one_element_in_subRegionArray = arr 

for i in stride(from: 0, to: arr.count, by: 2) { 
    Boundary_points_in_string_format += [String(arr[i] + "," + arr[i+1])] 
}