Here is a part of my URL
Building a dictionary of [city:id] can be a solution for you. I have implemented a simple solution based on the article about lifecycle of NSXMLParser at http://www.codeproject.com/Articles/248883/Objective-C-Fundamentals-NSXMLParser .
Following method is called when when an element is starting. You can retrieve city id attribute and save it in an instance level variable so that you can use it in the next method.
func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [NSObject : AnyObject])
And then, Following method is called when the parser see anything between starting and ending.
func parser(parser: NSXMLParser!, foundCharacters string: String!)
So, you can get the city name from here. Now we have city id and city name to add a new item into the [city:id] dictionary.
Once you build the dictionary, searching would be very simple.
here is my working test code.
class ViewController: UIViewController ,NSXMLParserDelegate{
var parser: NSXMLParser!
var city: String = String()
var ifDirOK = false
var ifCityNameOK = false
var element : String?
var value: String=String()
var dic = Dictionary<String,String>()
var currentCityId:String?
@IBOutlet weak var result: UILabel!
@IBOutlet weak var search: UITextField! //search text
@IBAction func ActionGoGetIt(sender: AnyObject) {
self.result.text=dic[self.search.text]
}
override func viewDidLoad() {
super.viewDidLoad()
let url: NSURL = NSURL(string: "https://pogoda.yandex.ru/static/cities.xml")!
parser = NSXMLParser(contentsOfURL: url)
parser.delegate = self
parser.parse()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [NSObject : AnyObject]) {
element = elementName
if (element == "city"){
ifDirOK = true
let cityID = attributeDict ["id"] as? NSString
self.currentCityId = cityID as? String
}
}
func parser(parser: NSXMLParser!, foundCharacters string: String!) {
var data = string.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
if (!data.isEmpty){
if (element == "city"){
dic[data] = self.currentCityId as String?
}
}
}
func parser(parser: NSXMLParser, foundAttributeDeclarationWithName attributeName: String, forElement elementName: String, type: String?, defaultValue: String?) {
if (ifDirOK && ifCityNameOK){
println("\(attributeName)")
}
}
func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
}
}