斯威夫特 - 在plist中的数组寻遍字典 [英] Swift - searching through dictionaries in an array in a plist

查看:223
本文介绍了斯威夫特 - 在plist中的数组寻遍字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个简单的应用程序,通过包含地理修复了大量的plist搜索,同时相应的纬度和经度坐标。该计划是要能够搜索的搜索框这些修补程序,然后按下一个按钮来定位MapView类在中心修复,并在准确的坐标引脚。

I'm making a simple app that searches through a large plist that contains geographical fixes, with corresponding lat and long coordinates. The plan is to be able to search for these fixes in a search box, and then push a button to position a mapview with the fix in the center, and a pin at the exact coordinates.

我有接口的设置,地图的工作和只需要启用该按钮以搜索修正了用户输入,返回纬度长,转发这些地图。我一直在寻找,现在通过回答了好几个小时,并试图什么,我没有运气..

I have the interface setup, the map working and just need to enable the button to search for the fix that the user inputs, return the lat long, and forward these to the map. I've been looking through answers on here for hours now and tried versions of what I found with no luck..

这是中的plist:

在这里输入的形象描述

有相当多的修正,你可以看到...什么会去对此的最好方法是什么?我不知如何从这里下去。

It is quite a lot of fixes as you can see... What would be the best way to go about this? I'm at a loss how to go on from here.

这是我使用的地图什么位置

This is what I'm using to position the map

func zoomToRegion() {
  let location = CLLocationCoordinate2D(latitude: 62, longitude: 16)
  let region = MKCoordinateRegionMakeWithDistance(location, 700000.0, 900000.0)

  mapView.setRegion(region, animated: true)
}

正如你现在看到的我刚才手动输入经纬度和长,但它的工作原理呢。

As you see right now I have just manually entered a lat and long but it works anyway.

我来使它的工作是本指南以下最接近:的 http://rshankar.com/how-to-add-mapview-annotation-and-draw-polyline-in-swift/ 不过,我不希望修复要上地图,直到有人搜索他们,我无法将其转换code做我想要的。

The closest I came to making it work was following this guide: http://rshankar.com/how-to-add-mapview-annotation-and-draw-polyline-in-swift/ However I don't want the fixes to be on the map until someone searches for them, and I was unable to convert that code to do what I wanted..

感谢任何帮助或洞察力这个!

Thanks for any help or insight into this!

推荐答案

我宁愿存储它们纬度和放大器;只要双,但无论如何,你应该首先从创建的.plist一个NSArray
距离的.plist 阅读)
然后过滤阵列,无论是存储在同一纬度和放大器;用户在键入的长期价值。

I'd rather store them lat & long as Double, but anyway, you should create an NSArray first from that .plist (Read from .plist) Then filter that array, whether it stores the same lat & long values that the user typed in.

guard let filePath = NSBundle.mainBundle().pathForResource("YOUR_FILE_NAME", ofType: "plist"),    
let locationsArray = NSArray(contentsOfFile:filePath) else {
    print("Couldn't load .plist as an array")
    return
}

// TODO: Replace them with the actual user input
let userInputText: String = "ALABA"

// It may not contain any items
let filteredLocations = locationsArray.filter { (element) -> Bool in
    guard let title = element["title"] as? String where
      userInputText == title else { return false }
    return true
}

// To convert String lat & long to Double
let numberFormatter = NSNumberFormatter()
numberFormatter.decimalSeparator = ","

// If you only need the first result:
guard let locationDict = filteredLocations.first as? [String:AnyObject],
    let latString = location["lat"] as? String, 
    let longString = location["long"] as? String,
    let latitude = numberFormatter.numberFromString(latString)?.doubleValue,
    let longitude = numberFormatter.numberFromString(longString)?.doubleValue else {
       print("No result"); return
    }

// Your location
let location = CLLocationCoordinate2D(
    latitude: latitude, 
    longitude: longitude
)

// Do something with your location
let region = MKCoordinateRegionMakeWithDistance(location, 700000.0, 900000.0)
mapView.setRegion(region, animated: true)

但它会是更好,如果你实现了一个类,像LocationData,随着给定的.plist的属性,而且你一箱[LocationData]数组,所以你可以到达纬度和放大器;长,一个对象的属性,而不是从字典键。

But it'd be nicer if you implemented a class, something like LocationData, with the properties given in the .plist, and you'd crate a [LocationData] array, so you could reach the lat & long by properties of an object, not by key from a dictionary.

这篇关于斯威夫特 - 在plist中的数组寻遍字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆