Some Swift libraries every iOS developer should know about

Posted By : Aditya Kumar Sharma | 28-Mar-2016

Swift Libraries - iOS
For making projects we generally use various libraries to make transition easier and save the time for writing certain components for our app, here are some libraries that every iOS developer should know about. We can find these open source iOS libraries on Github and  Bitbucket. For installing and maintaining libraries in our app we can use tools like CocoaPods  and  Carthage.

1. Alamofire

Alamofire is a HTTP networking library, built on top of NSURLSession and the Foundation URL Loading System. It can be used when we want to abstract away and simplify networking in our app. It wraps networking with Swift interface nicely.

Alamofire.request(.GET, "http://localhost:3000/", parameters: ["param": "parameter"])
         .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)")
             }
         }
 

2. SwiftyJSON 

SwiftyJSON helps to deal with the Explicit types in Swift so that we don't make mistakes and cause bugs. SwiftyJSON is making JSON data more readable in Swift. Optional unwrapping is handled automatically as well!

// Typical JSON handling

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

// With SwiftyJSON

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

SwiftyJSON also work easily 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)
    }
} 

3. ObjectMapper

 If we have to write an app which downloads information via an API, we can use ObjectMapper. Object Mapper helps to convert a JSON response into our model object and vice versa. It helps to map JSON to objects and objects back to JSON.

// Location class that conforms to Mappable protocol
struct Location: Mappable {
    var longitude: Double?
    var latitude: Double?
    init?(_ map: Map) {
    }
    mutating func mapping(map: Map) {
        longitude     <- map["longitude"]
        latitude  <- map["latitude"]
    }
} 

4. SnapKit

 SnapKit is an Auto Layout library which simplifies writing auto layout in code with a minimum amount of code needed without losing readability. It help us to avoid programming erors while coding our user interface.

// Resizing and centering subview in its superview

let contentView = UIView()
let container = UIView()

container.addSubview(contentView)

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

5. Spring

Spring is an animation librarywhich helps to create animations using or through Storyboard directly. We can create animations using runtime attributes in Storyboard. Spring is fully developed animation library that supports animations, transitions, and properties.

// Usage with code
layer.animation = "flash"
layer.animate()
 

6. Kingfisher

Kingfisher is a library for downloading and caching images from the web which is done asynchronously. Images are cached both in memory and disk,which imporves app experience.

// Basic example of downloading and caching an image
imageView.kf_setImageWithURL(NSURL(string: "http://image_url.png")!) 

7. CoreStore

 CoreStore is a wrapper library for Core Data. Its aim is to provide safety of Swift when using Core Data. It provides all the methods to effectively interact with database.

// Storing a person entity

CoreStore.beginAsynchronous { (transaction) -> Void in
    let person = transaction.create(Into(MyPersonEntity))
    person.name = "Lorem ipsum"
    person.age = 32

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

THANKS

About Author

Author Image
Aditya Kumar Sharma

Aditya is a bright iOS developer, have knowledge of objective C, swift, swift 3, JSON, Core data and iPhone development. Apart from that he loves to travel and explore new things.

Request for Proposal

Name is required

Comment is required

Sending message..