gpt4 book ai didi

ios - 无法将类型 '[String]' 的值分配给类型“String”? swift 2

转载 作者:行者123 更新时间:2023-11-29 00:49:56 24 4
gpt4 key购买 nike

我收到与“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)
}}}

这现在正在正确运行或至少报告正确的值,但循环只是保持循环...

最佳答案

试试这个:

//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 aPlace in place {
for aPass 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 = aPlace
point.subtitle = aPass

mapView.addAnnotation(point)
}}}

//var i and x for loop
}

//viewDidLoad
}
}

关于ios - 无法将类型 '[String]' 的值分配给类型“String”? swift 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38256955/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com