在迭代数组swift时出错

我想循环访问数组并获取坐标,但它给了我一个错误,我无法访问我的数组。数据通过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

}

}

回答:

一个结构,一个数组(而不是解释),所以你需要做的就是重复这样的:

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功能,您可以指定文本将如何看,当你打印对象(或这些对象的数组),如

以上是 在迭代数组swift时出错 的全部内容, 来源链接: utcz.com/qa/265964.html

回到顶部