Search code examples
xmlswiftswxmlhash

Parse XML request in Swift


I am just beginning to dabble around with Swift.

I have the following get request using NSURLSession:

let url = NSURL(string: "http://api.bart.gov/api/sched.aspx?cmd=arrive&orig=24th&dest=rock&key=MW9S-E7SL-26DU-VV8V")

let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in

        println(NSString(data: data, encoding: NSUTF8StringEncoding))

          }

    task.resume()

However, the data is all jumbled up in XML. How do I go about formatting the data in a readable manner?

I am trying to parse the data using this line of code:

let xml = SWXMLHash.parse(data: data)

However, this is giving me an error about not being able to use data. Am I headed in the right direction with the SWXMLHash.Parse function?


Solution

  • The syntax for the parse method is without data: label:

    let xml = SWXMLHash.parse(data)