Top 10 iOS Swift Libraries Every iOS Developer Should Know About

top-10-ios-swift-libraries-every-ios-developer-should-know-about-0

Swift is gaining popularity each day. If you’re starting a new project, chances are that you’ll decide to write it in Swift. To make the transition easier for you and save you the time you would spend writing certain components for your app, here are 10 libraries that we think every iOS developer should know about!

Just like we mentioned in our Top 5 iOS libraries every iOS developer should know about article, GitHub and Bitbucket are great places to find open source iOS libraries. Tools like CocoaPods and Carthage can help you speed up installing and maintaining libraries you use in your projects, and in that way make project dependency management easy for you.

Top 10 Swift libraries

When you want to abstract away and simplify networking in your app, Alamofire is the way to go. Alamofire is a HTTP networking library, built on top of NSURLSession and the Foundation URL Loading System. It nicely wraps networking mechanisms in an elegant and simple Swift interface.

	// Making a GET request

Alamofire.request(.GET, "https://httpbin.org/get", parameters: ["foo": "bar"])
         .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)")
             }
         }

Explicit types in Swift make sure that we don’t make mistakes in our code and cause bugs because of them. But sometimes it is rather painful to deal with it, especially when working with JSON. Luckily, SwiftyJSON is here to help us deal with JSON data in Swift in a more readable way. Optional unwrapping is handled automatically for you as well!

	// Typical JSON handling

if let statusesArray = try? NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments) as? [[String: AnyObject]],
    let user = statusesArray[0]["user"] as? [String: AnyObject],
    let username = user["name"] as? String {
    // Finally we got the username
}

// With SwiftyJSON

let json = JSON(data: dataFromNetworking)
if let userName = json[0]["user"]["name"].string {
  //Now you got your value
}

SwiftyJSON also plays very nice with Alamofire.

	Alamofire.request(.GET, url).validate().responseJSON { response in
    switch response.result {
    case .Success:
        if let value = response.result.value {
          let json = JSON(value)
          print("JSON: \(json)")
        }
    case .Failure(let error):
        print(error)
    }
}

If you’ve ever written an app which downloads information via an API, you’ve probably spent a lot of time writing a code to map a response to your objects. ObjectMapper helps you convert a JSON response into your model object, and vice versa. In other words, it helps you map JSON to objects, and objects back to JSON. Nested objects are supported as well.

	// Temperature class that conforms to Mappable protocol

struct Temperature: Mappable {
    var celsius: Double?
    var fahrenheit: Double?

    init?(_ map: Map) {

    }

    mutating func mapping(map: Map) {
        celsius     <- map["celsius"]
        fahrenheit  <- map["fahrenheit"]
    }
}

It’s also worth to mention AlamofireObjectMapper, an Alamofire extension which converts JSON response data into Swift objects when using ObjectMapper.

4. Quick

Quick is a behavior-driven development framework for Swift, inspired by RSpec, Specta, and Ginkgo. Quick comes with Nimble, which is a matcher framework for your tests.

	// Documentation directory spec

class TableOfContentsSpec: QuickSpec {
  override func spec() {
    describe("the ’Documentation’ directory") {
      it("has everything you need to get started") {
        let sections = Directory("Documentation").sections
        expect(sections).to(contain("Organized Tests with Quick Examples and Example Groups"))
        expect(sections).to(contain("Installing Quick"))
      }

      context("if it doesn’t have what you’re looking for") {
        it("needs to be updated") {
          let you = You(awesome: true)
          expect{you.submittedAnIssue}.toEventually(beTruthy())
        }
      }
    }
  }
}

5. Eureka

Eureka helps you write dynamic table-view forms in a simple and elegant way. It consists of rows, sections and forms. If your app contains a lot of forms, Eureka proves to be a real time-saver.

	// Creating a form

class CustomCellsController : FormViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        form +++ Section("Custom cells")
                    <<< WeekDayRow(){
                        $0.value = [.Monday, .Wednesday, .Friday]
                    }
                    <<< TextFloatLabelRow() {
                        $0.title = "Float Label Row, type something to see.."
                    }
    }
}

RxSwift is a Swift framework for Functional Reactive Programming. To be more specific, RxSwift is a Swift version of Rx and its goal is to enable easy composition of asynchronous operations and event/data streams. KVO observing, async operations and delegates are all unified under abstraction of sequence, which makes RxSwift such a powerful framework. If you’ve ever worked with ReactiveCocoa, you are familiar with the concept.

	// Combine first and last name sequences, and bind the resulting string to label

combineLatest(firstName.rx_text, lastName.rx_text) { $0 + " " + $1 }
            .map { "Greeting \($0)" }
            .bindTo(greetingLabel.rx_text)

SnapKit is an Auto Layout library that simplifies writing auto layout in code with a minimal amount of code needed without losing readability. It is type safe by design to help you avoid programming errors while coding your user interface.

	// Resizing and centering subview in its superview

let box = UIView()
let container = UIView()

container.addSubview(box)

box.snp_makeConstraints { (make) -> Void in
    make.size.equalTo(50)
    make.center.equalTo(container)
}

8. Spring

Spring is an animation library that helps you create animations both in code or directly in Storyboard. You can create animations in Storyboard using runtime attributes (set through IBInspectable properties). Spring has grown into a fully developed animation library that supports quite a number of already written animations, transitions, and properties.

	// Usage with code

layer.animation = "wobble"
layer.animate()

Kingfisher is a lightweight library for downloading and caching images from the web. Downloading and caching is done asynchronously. Downloaded images are cached in both memory and disk, which could improve your app experience quite a lot.

	// Basic example of downloading and caching an image

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

CoreStore is a wrapper library for Core Data. Its goal is to provide type safety and elegance of Swift when interacting with Core Data. CoreStore’s API provides all common methods to effectively interact with your database.

	// Storing a person entity

CoreStore.beginAsynchronous { (transaction) -> Void in
    let person = transaction.create(Into(MyPersonEntity))
    person.name = "John Smith"
    person.age = 42

    transaction.commit { (result) -> Void in
        switch result {
            case .Success(let hasChanges): print("success!")
            case .Failure(let error): print(error)
        }
    }
}

What’s your opinion?

You don’t agree with us? Some other library deserves a place in this top 10 list? Leave a comment and let us know what you think!