我正在将我的项目更新为Swift 3.大多数都进展顺利,但我无法想象下面的内容。我相信我已经找到了如何纠正这两个错误中的第一个,但是无法弄清楚第二个错误" i = i + 1"因为我收到了错误"无法分配价值:'我'是一个“让...”恒定"
我雇了一个人在我的应用程序上做坐标,所以这就是为什么我不习惯这些类型的错误。
我创建了200个多边形区域,下面这是为了获取用户当前位置并确定它们所在的区域并使用该区域的数据。
我看到了这个问题,但看不出这是如何回答我的问题的。 Cannot assign to value: 'i' is a 'let' constant in swift
set DEBUG=myapp:* & npm start
由于
更新了课程中的更多数据。 @Alexander Momchliov,我更新了"坏"带有代码的区域,但是出现一个错误
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
}
错误是&#34;无法转换值类型&#39;(字符串)&#39; - &GT; [CLLocaitonCoordinate2D]&#39;期望参数类型&#34; CLLocationCoordinate2D&#39;&#34;并指向&amp; boundry
let polygon = MKPolygon(coordinates: &boundary, count: boundary.count)
答案 0 :(得分: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
}
var boundary: [CLLocationCoordinate2D]
和boundary = []
,只需使用var boundary = [CLLocationCoordinate2D]()
...
)时,不要手动减去一个。只需使用闭区域运算符(..<
)。例如0..<boundaryPointsCount
代替0...boundaryPointsCount-1
0..<array.count
array.indices
醇>
答案 1 :(得分:1)
索引变量根据定义是常量,您无法更改它们。
但是,如果您想在重复循环中使用item
加item + 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])]
}
答案 2 :(得分:0)
就像我看到的那样,你只是递增i
因为你从旧的for循环风格转换了。在使用枚举的for in中,您不需要增加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()
,你会在for之外创建一个Int
,然后递增它。您可以不更改一个for循环中的项目。