我正在更新我的項目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
}
*/
謝謝,試過了,但得到的指數列於Boundary_points_in_string_format + = [字符串(Array_of_one_element_in_subRegionArray [誤差範圍i] +「,」+ Array_of_one_element_in_subRegionArray [i + 1])] –
請考慮使用Swift命名約定:變量名始終以小寫字母開頭並且是camelCased。這不是JavaScript或PHP。你的代碼很難閱讀。 – vadian
這真的很難看,但我給它一個鏡頭。我會很快回復 – Alexander