I am new to Swift, writing my first application with network requests. For testing purposes, I created an application with the frameworks Alamofire and SWXMLHash for parsing the xml-data from the request.
In general, it works fine (more or less, I don't know how to use a completionHandler for Alamofire and I don't even understand what that really is... shame on me)
But now it's the first time that my xml-Structure has multiple Items of the same name and I want to iterate all of them to an Array using their "Name" attribute.
Let me give you an example for the XML:
<Root>
<Results>
<Url Name="Test1"></Url>
<Url Name="Test2"></Url>
<Url Name="Test3"></Url>
</Results>
</Root>
So my Array should be like this:
["Test1", "Test2", "Test3"]
my code for this so far is not working... I get an error that unwrapping the xml data returns nil. Here is the Code of the whole file:
import WatchKit
import Foundation
import Alamofire
import SWXMLHash
public var url = "http://www.testurl.com"
public var user = "testuser"
class InterfaceController: WKInterfaceController {
@IBAction func selectedAccountActivate() {
campaignLister()
}
func campaignLister(){
Alamofire.request(.GET, url, parameters: ["function":"catalog", "user":user, "pwd":"1234"])
.response { (request, response, data, error) in
//print(response)
let xml = SWXMLHash.parse(data!)
var xml2: [String] = []
for elem in xml["Root"]["Results"] {
/The following line gives me the error. It works when I make a request where I only have one "URL" element, but as soon as I have many like in this example, my app crashes/
xml2.append(elem["Url"].element!.attributes["Name"]!) } print(xml2) } }
override func willActivate() {
super.willActivate()
}
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
}
override func didDeactivate() {
super.didDeactivate()
}
}
Can anyone help me out? I used Google a lot for this problem, but I didn't find any solutions for this. Especially because I am really new to Swift...
Best regards,
hallleron
(Sorry for grammar mistakes you may find, english is not my native language.)