【发布时间】:2016-07-08 08:50:14
【问题描述】:
我收到与此行“point.title = r”和此行“point.subtitle = z”相关联的错误消息“无法将类型'[String]' 的值分配给类型'String?'”。我已经尝试了很多类似 place.description 的方法,它使整个数组成为一个字符串...
//I have multiple arrays, lat, long and pass besides place.
var place = [String]()
//I have four of the chunks of code for each array
let json = try NSJSONSerialization.JSONObjectWithData(jSONData, options: .AllowFragments)
if let dataFile3 = json["data"] as? [[String: AnyObject]] {
for c in dataFile3 {
if let placeName = c["PlaceName"] {
place.append((placeName as? String)!)
for var (i,x) in zip(lat, long) {
for _ in place {
for _ in pass {
print(i)
print(x)
//print(i + ", " + x)
// String to double conversion, can I do this when I set the array as a string?
let lati = NSString(string: i).doubleValue
let longi = NSString(string: x).doubleValue
//I have a list of values array so I need to have a for loop that inputs the value over and over again.
var point = MGLPointAnnotation()
point.coordinate = CLLocationCoordinate2D(latitude: lati, longitude: longi)
point.title = place
point.subtitle = pass
mapView.addAnnotation(point)
}}}
//var i and x for loop
}
//viewDidLoad
}
//creates the markers popup for each point with data
func mapView(mapView: MGLMapView, annotationCanShowCallout annotation: MGLAnnotation) -> Bool {
// Always try to show a callout when an annotation is tapped.
return true
}
如何解决上述错误?
编辑: 在 Santosh 的帮助下,我改变了这一切……
let point = MGLPointAnnotation()
var i = lat
var x = long
//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 (i,x) in zip(lat, long) {
for aPlace in place {
for aPass in pass {
print(i)
print(x)
// String to double conversion, can I do this when I set the array as a string?
let lati = NSString(string: i).doubleValue
let longi = NSString(string: x).doubleValue
point.coordinate = CLLocationCoordinate2D(latitude: lati, longitude: longi)
point.title = aPlace
print(aPlace)
point.subtitle = aPass
print(aPass)
self.mapView.addAnnotation(point)
}}}
现在运行正常,或者至少报告了正确的值,但循环只是保持循环......
【问题讨论】:
-
place是一个array,因此您需要从该数组中访问该项目。因为point.title接受optional String,即String?。这就是错误所说的。 -
是的,这基本上就是我的想法,只是不知道如何解决它......我在下面使用你的建议,但不幸的是我认为它没有任何区别。我非常感谢您的努力。
-
你在下面的答案中遇到了什么错误?
-
在上面添加了一个新块供您查看。需要在 105 条记录后停止循环。
-
我看到了 3 个循环,因此请检查哪个循环具有更多值并将其限制为您想要的任何值。