如何使用多个数组设置 for 循环作为变量以用作 Swift 中标记中的文本 2
How do I set up a for loop with multiple arrays as variables to serve as text in a marker in Swift 2
我有多个数组,我想遍历这些数组,稍后将它们作为值插入我的代码中以用于地图上的标记。我填充了标记,但不知道如何将文本标记标题和副标题应用到与位置关联的标记。
iOS 和 Swift 2
编辑:也使用 Mapbox iOS SDK。
如何编写执行此操作的 for 循环?
到目前为止,我的代码已精简。
从 geojson 文件中获取的数组:
//place variable to array of strings
var place = [String]()
//result from this array ["park", "playground", "parking"]
//pass variable to array of strings
var pass = [String]()
//result from this array ["True", "False", ""]
我也用纬度和经度做了这个,但不会在这里添加。但是我确实需要为它添加for循环。
//lat and long is served and then all the r,z points, then another lat, long...need to fix this and it may work...
for var (i,x) in zip(lat, long) {
print(i)
print(x)
//print(i + ", " + x)
// String to double conversion
let lati = NSString(string: i).doubleValue
let longi = NSString(string: x).doubleValue
var point = MGLPointAnnotation()
point.coordinate = CLLocationCoordinate2D(latitude: lati, longitude: longi)
let r = place
print(r)
point.title = r
print(r)
let z = pass
print(z)
point.subtitle = z
mapView.addAnnotation(point)
}
因此标记都已根据纬度和经度正确添加到地图中。然而,标记文本仅添加一次,并使用最后提供的记录。我打赌我在这里遗漏了一些简单的东西,因为我对使用 Swift 2.
进行编码很陌生
我收到与此行 "point.title = r" 和此行 "point.subtitle = z" 相关的错误消息 "Cannot assign value of type '[String]' to type 'String?'",这使事情变得复杂。
如有任何建议,我们将不胜感激。
如果您需要更多信息,我可以根据需要进行编辑,但希望您拥有帮助我设置此 for 循环所需的一切。
point.title和point.subtitle中标题和副标题的数据类型分别是什么?
您正在将 String Array 位置分配给 r。而 point.title 不是数组类型。我仍然想念你想在哪里添加循环的问题?
我有多个数组,我想遍历这些数组,稍后将它们作为值插入我的代码中以用于地图上的标记。我填充了标记,但不知道如何将文本标记标题和副标题应用到与位置关联的标记。
iOS 和 Swift 2
编辑:也使用 Mapbox iOS SDK。
如何编写执行此操作的 for 循环?
到目前为止,我的代码已精简。
从 geojson 文件中获取的数组:
//place variable to array of strings
var place = [String]()
//result from this array ["park", "playground", "parking"]
//pass variable to array of strings
var pass = [String]()
//result from this array ["True", "False", ""]
我也用纬度和经度做了这个,但不会在这里添加。但是我确实需要为它添加for循环。
//lat and long is served and then all the r,z points, then another lat, long...need to fix this and it may work...
for var (i,x) in zip(lat, long) {
print(i)
print(x)
//print(i + ", " + x)
// String to double conversion
let lati = NSString(string: i).doubleValue
let longi = NSString(string: x).doubleValue
var point = MGLPointAnnotation()
point.coordinate = CLLocationCoordinate2D(latitude: lati, longitude: longi)
let r = place
print(r)
point.title = r
print(r)
let z = pass
print(z)
point.subtitle = z
mapView.addAnnotation(point)
}
因此标记都已根据纬度和经度正确添加到地图中。然而,标记文本仅添加一次,并使用最后提供的记录。我打赌我在这里遗漏了一些简单的东西,因为我对使用 Swift 2.
进行编码很陌生我收到与此行 "point.title = r" 和此行 "point.subtitle = z" 相关的错误消息 "Cannot assign value of type '[String]' to type 'String?'",这使事情变得复杂。
如有任何建议,我们将不胜感激。
如果您需要更多信息,我可以根据需要进行编辑,但希望您拥有帮助我设置此 for 循环所需的一切。
point.title和point.subtitle中标题和副标题的数据类型分别是什么? 您正在将 String Array 位置分配给 r。而 point.title 不是数组类型。我仍然想念你想在哪里添加循环的问题?