2016-07-07 8 views
0

Я получаю это сообщение об ошибке «Невозможно присвоить значение типа« [String] »для ввода« String? »», Связанного с этой строкой «point.title = r», и этой строки «point.subtitle = z». Я пробовал много вещей, как place.description, что делает весь массив строку ...Нельзя присвоить значение типа '[String]' для ввода 'String? Swift 2

//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 
    } 

Как исправить ошибку выше?

EDIT: С помощью 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) 
      }}} 

Это правильно работает сейчас или, по крайней мере отчетности правильные значения, но цикл только сохраняет loopin ...

+0

'place' является' array', поэтому вам нужно получить доступ к элементу из этого массива. Поскольку 'point.title' принимает' необязательный String', который является 'String?'. И об этом говорит ошибка. – Santosh

+0

Да, это в основном то, о чем я думал, просто не знаю, как это исправить ... Я использую ваши предложения ниже, но я не думаю, что это имело какое-то значение, к сожалению. Я ценю ваши усилия. – Staley

+0

Какая ошибка вы получили в ответ ниже? – Santosh

ответ

1

Попробуйте следующее:

//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 
     } 
} 
Смежные вопросы