快速遍历数组并从元素中获取数据

时间:2019-10-26 12:52:19

标签: swift xcode

希望有这样的代码

var latitude: String?

 var longitude: String?

 func getMyCitiesCordinates(city:String){
  if city == "Adelaide"{
  self.latitude = -34.9286600
  self.longitude = 138.5986300
  }

但不要做

如果城市==

为每个城市获取经度和纬度,我就像下面的函数一样,它在数组中搜索匹配的City / String并返回该城市的纬度和经度?

 func getLatAndLong(myCity:String){

  let citiesArray = ["Adelaide": CLLocation(latitude: -34.9286600, longitude: 138.5986300),"Albury": CLLocation(latitude: -36.0737, longitude: 146.9135),"Alice Springs": CLLocation(latitude: -23.6980, longitude: 133.8807),"Armadale": CLLocation(latitude: -32.1530, longitude: 116.0150),"Ballarat": CLLocation(latitude: -37.5622, longitude: 143.8503),"Brisbane": CLLocation(latitude: -27.470125, longitude: 153.021072),"Bunbury": CLLocation(latitude: -33.3256, longitude: 115.6396)]
}

然后返回那个城市的纬度和经度? 纬度=? 经度=?

1 个答案:

答案 0 :(得分:-1)

   class city: NSObject {
       var city: String
       var longitude: Double?
       var latitude: Double?

        init(city: String, latitude: Double?, longitude: Double?) {
         self.city = city
         self.longitude = longitude
         self.latitude = latitude
        }
   }
   var latitude: Double?
   var dict: [String:CLLocation]
   var longitude: Double?
   var dict = citiesArray


   func viewDidLoad {
   city = getMyCitiesCordinates(mycity:"Adelaide")
   }



    func getMyCitiesCordinates(mycity:String) -> city{

          let location = dict[mycity]
          return city(city: mycity, latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)

      }