How I boosted my iOS development with Swift Libraries

Datetime:2016-08-23 04:15:33          Topic: Swift           Share

As an iOS developer working at a startup focused in collaborative development, I’ve been involved in several projects so far, and most of them share common tasks such as downloading and caching images, performing network requests, and building Auto Layout views.

At first, I had a flow that I thought was good enough to accomplish these basic tasks (or any other, for that matter):

  1. Try to implement using the iOS SDK
  2. Get stuck at a problem that doesn’t have a straightforward solution
  3. Look up the solution on StackOverflow and implement it
  4. Move on to the next task

That seemed like a good flow at first, but got a bit tiresome in the long run – after all, nobody likes to hack for a living.

This all changed when I started reading a couple of widely followed news feeds such as iOS Dev Weekly , and also checking GitHub Trending Repositories . Looking at the these sources regularly, I realized that most of the basic tasks I need to accomplish on a daily basis have already been solved by the open-source community – and are available in the form of libraries.

Overall, these tools boosted my development by reducing boilerplate and also by addressing my problems better than I (or anyone) could in almost no time – which is a key factor in any startup business. Don’t get me wrong here – I’m not assuming you’re a lousy dev, but using tools that were validated by a bunch of people may come in really handy.

I’ve put together some of my favorite Swift libraries that I use on a daily basis to solve basic problems.

1. Downloading and Caching Images

Images are present in almost all apps I’ve seen so far, even if it’s just a profile picture in the settings screen. Combining NSURLSession and NSURLCache to respectively download and cache the images can definitely do the job, but why bother with all that if you’re able to use Kingfisher (a Swift-flavoured SDWebImage ) and set the image of your ImageView within a single line? Check this out:

imageView.kf_setImageWithURL(NSURL(string: "http://your_image_url.png")!)

It also provides simple ways to define placeholder images, completion blocks and even check the download progress.

2. Performing Network Requests

Games storing user content, news feeds providing up-to-date content, chat apps interfacing the message exchange and so much more – nowadays it’s nearly impossible to imagine an app that does not communicate with a server through an HTTP(S) API. If you ever had tons of headaches to fetch and parse data from an API, here is the solution for you my friend: Alamofire, a Swift version for AFNetworking , developed and maintained by the same team.

Alamofire.request(.GET, "https://httpbin.org/get", parameters: ["foo": "bar"])
    .validate()
    .responseJSON { response in
        print(response.request)  // original URL request
        print(response.response) // URL response
        print(response.data)     // server data
        print(response.result)   // result of response serialization
        if let JSON = response.result.value {
            print("JSON: \(JSON)")
        }
    }

It does all sorts of things, from uploading files to response validation, in a very elegant way.

3. Building Auto Layout Views

If you have ever had to work with NSLayoutConstraint (or worse VFL o.O) you know how painful it can be to implement even the most simple views programmatically. After years of complaints from the community, Apple has finally provided a fluent Auto Layout API for iOS9 and up, but if your app still has to support iOS8,  SnapKit (a Swift version of Masonry , developed and maintained by the same team) offers the best solution out there.

import SnapKit
class MyViewController: UIViewController {
   lazy var box = UIView()
   override func viewDidLoad() {
       super.viewDidLoad()
       self.view.addSubview(box)
       box.snp_makeConstraints { make in
          make.width.height.equalTo(50)
          make.center.equalTo(self.view)
       }
   }
}

The Shift to Swift

If you pay attention to the most famous libraries out there, you’ll realize how the community is moving from Objective-C to Swift more and more every day. Most of the big companies are moving on that direction as well – a great example is Facebook, which already has a beta version of its Facebook SDK in Swift .

Another boost to this trend lies on Apple working on its own alternative to Cocoapods and Carthage, the Swift Package Manager . According to Ben Morrow in What’s New In Swift 3 , it will be the first release to include the SPM. There are several libraries that already support it and we’ll likely start to see even more in the coming months.

Whether you decide to keep your Objective-C or move on to Swift, I hope these libraries help you as much as they have helped me so far. Agree, disagree or have any suggestions? Let me know your thoughts!





About List