Parsing an Alamofire request in swift

There are a lot of reasons for parsing an Alamofire request in swift. I found this procedure very useful if you need to get some response from an XML after an Alamofire REST request.

All you need is Alamofire and SwiftyXMLParser (you can parse it manually with the native swift code but using this pod will save you a lot of time). Once you’ve loaded you pods you can proceed by simply declaring a function and follow the code that you can find below.

In this example the Alamofire.request will contact the url “http://www.apple.com/works_for_us.php” and send the parameters declared before as GET (so the final result will be something like http://www.apple.com/works_for_us?name=Tim&second_name=Cook&company=Apple).

As soon as the code behind that page will provide the xml page we are able to parse it and provide a response. In the code below you will see the XML generated by the page that we have just loaded.

<root>
<result code="1" msg="He works here"/>
</root>

In this case the XML parameters “code” is worth “1” so we can comple the function with a “true” answer (see the completion(true) part).