Useful libraries for SWIFT beginners
Recently we started working with Apple Swift language. It’s pretty simple but very power full language. Learning curve is hard – because it’s pretty new language, but from the other hand it’s good – because there is a lot of freshmen questions on StackOverflow and plenty of articles .
Because most of the stuff is related with REST communication with APIs core of my software works with JSON objects. In general we are looking also something for HTTP communication, uploading files and so on.
Parsing JSON in SWIFT
Let’s start with JSON – json-swift and SwiftyJSON .
Both of them are giving super easy wrappers around parsing JSON streams.
Instead of
let JSONObject: AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: nil)
if let username = (((JSONObject as? [AnyObject])?[0] as? [String: AnyObject])?["user"] as? [String: AnyObject])?["name"] as? String {
// ... do your job
}
With:Swifty
let json = JSON(data: dataFromNetworking)
if let userName = json[0]["user"]["name"].string{
// ... much cleaner
}
Making REST API Calls
Alamofire is an HTTP networking library written in Swift.
Calls are so easy as:
request(.GET, "http://mygetrequesturl.com").responseString { (_, _, string, _) in
println(string)
Uploading file directly from imagePickerController
And small bonus. I wanted to upload image directly to from imagePickerController – here is a recipe.