2017-12-27 248 views
2

我想循環訪問數組並獲取座標,但它給了我一個錯誤,我無法訪問我的數組。數據通過seque傳遞,如果我打印它顯示它正在工作,所以數據正確傳輸,但無法解決爲什麼我不能遍歷數組並拉出經度和緯度。在迭代數組swift時出錯

這是控制檯中的輸出。

DeliveryDestinations(NameOrBusiness:可選( 「In-N-Out漢堡」), FirstLineAddress:可選( 「550紐荷爾博士」),SecondLineAddress: 可選(」美國 「),CityLineAddress:可選(」聖荷西 「), PostcodeLineAddress:可選(」 CA 95110" ),DistanceToDestination: 可選(9.2807823200000001),緯度:可選(37.350253606833043),長: 可選(-121.92182779312132))

import UIKit 
import MapKit 
import CoreLocation 

class DeliveryLocationsVC: UIViewController { 

    @IBOutlet weak var mapView: MKMapView! 

    var addressArr = [DeliveryDestinations]() 


    override func viewDidLoad() { 
     super.viewDidLoad() 

     print(addressArr) 


navigationItem.title = "Delivery Location" 
     // Do any additional setup after loading the view. 

     for (theKey, theValue) in addressArr { //<-- error can't access the elements in array 
      if (theKey == "Lat") { 
       if let coordinates:DeliveryDestinations = theValue { 
       print (coordinates) 
      } 
      print("Item \(theKey): \(theValue)") 
      } 
     } 
    } 
} 

我數組即將到來fr OM下面

import Foundation 
import MapKit 

struct DeliveryDestinations { 
    var NameOrBusiness: String? 
    var FirstLineAddress: String? 
    var SecondLineAddress: String? 
    var CityLineAddress: String? 
    var PostcodeLineAddress: String? 
    var DistanceToDestination: CLLocationDistance? 
    var Lat: Double? 
    var Long: Double? 


    init(NameOrBusiness: String?, FirstLineAddress: String?, SecondLineAddress: String?, CityLineAddress: String?, PostCodeLineAddress: String?, DistanceToDestination: CLLocationDistance?, Lat: Double?, Long: Double?) { 
     self.NameOrBusiness = NameOrBusiness 
     self.FirstLineAddress = FirstLineAddress 
     self.SecondLineAddress = SecondLineAddress 
     self.CityLineAddress = CityLineAddress 
     self.PostcodeLineAddress = PostCodeLineAddress 
     self.DistanceToDestination = DistanceToDestination 
     self.Lat = Lat 
     self.Long = Long 

    } 

} 
+0

你有什麼期望'theKey'和'theValue'遏制? – luk2302

+0

@ luk2302鍵將爲Lat並且值將是相同的座標,長度將是鍵,值將是座標。試着把它放到數組中,這樣我就可以根據這個座標用引腳填充地圖。 – QuickSilver

+0

爲什麼'theKey'是「Lat」?這沒有任何意義,你有一個數組,沒有任何地方可以找到「Lat」。 – luk2302

回答

3

一個結構,一個數組(而不是解釋),所以你需要做的就是重複這樣的:

for object in addressArr { 
    ... 
    // get coordinates from object 
    let lat = object.Lat 
    let long = object.Long 
} 

,或者您需要一個錶行的索引,以及你可以這樣做:

for (i, object) in addressArr.enumerated() { 
    ... 
    // get coordinates from object 
    let lat = object.Lat 
    let long = object.Long 
    print ("Destination at index \(i) has coordinate: (\(lat), \(long))") 
} 

我怎麼知道這是一個數組?

符號中的代碼

var addressArr = [DeliveryDestinations]() 

意味着永諾,這是對象DeliveryDestinations的陣列。 當您打印出來時,您會打印陣列中的對象。 在你的情況下,這個對象有一個「像字典」的說明,所以打印功能打印它像這樣。

在每一個對象,具有description功能,您可以指定文本將如何看,當你打印對象(或這些對象的數組),如

+0

Zedravec這對我來說非常合適,兩種解決方案都非常完美。一個問題是我在哪裏搞砸了你怎麼知道這是一個數組而不是字典,因爲在這裏我感到困惑。緯度:可選(37.350253606833043),長:可選(-121.92182779312132))這不是鍵值對嗎? – QuickSilver

+2

@QuickSilver'var addressArr = [DeliveryDestinations]()',這是一個數組,句號。無論它包含什麼,以及這些東西如何決定將其自身轉換爲字符串,它仍然是一個數組。 – luk2302

+0

@ luk2302解釋對不起,我很新,所以我仍然在學習。非常感謝你的幫助。 – QuickSilver