How to Use RestKit in Swift for iOS Development

by Zymr

Share It

RestKit in Swift for iOS Development


iOS developers nowadays are eager to use Swift language in their live projects, so I will be introducing a development accelerator module for Swift in this post. I have found a way to use RestKit in Swift. Hope all you software engineers and app developers enjoy this post and can use the information for your own iOS app development.

Create Project & Install RestKit

Create a Single View Application Project, and make sure that you enable Core Data while creating the project. We need a bridging header to communicate with Objective-c and Swift. More information Here. Then Integrate RestKit from Cocoapods. More information Here.  If you install Restkit before enabling bridging header, then Swift and Object-C cannot work well together.

In Bridging-Header.h import Restkit header classes:

#import <CoreData/CoreData.h>

#import <RestKit/RestKit/RestKit.h>

#import <RestKit/CoreData.h>

Now you can build the project. Everything looks good up to this point so you can start your work.

[See Also: iOS WebSocket: Old Fashioned, New Style]

Entity and Mapping

Now it is time to add the Utility stuff. Create Constant.swift by adding:

let kBase_URL:String = “”

let kDB_store:String = “xxxxx.sqlite”

Create Enum.swift by adding:

enum OM_WS {   case LOGIN, GET_USERS}

Now edit in XCDatamodel by creating the required entity. Here I am going to add an Authenticate Entity, which will
have a token property as a String.


Create this entity to NSManagedObject as we did normally. Make sure you select the language as Swift. Your class should look like:

@objc (Authenticate)

class Authenticate: NSManagedObject {

@NSManaged var token: String


The interesting fact about coding this class is that we are not going to write any mapping methods. For mapping,
create another swift class for maintaining mapping. It is my advice to create DataObjectMapping.swift for cleaner code. Your class should look like:

class DataObjectMapping: NSObject {

//MARK: Authentication Mapping

func objectMappingForAuth(oppCode: OM_WS) -> RKEntityMapping {

let appDelegate = UIApplication.sharedApplication().delegate as!

var mapping: RKEntityMapping  =

RKEntityMapping(forEntityForName: “Authenticate”,

mapping.setDefaultValueForMissingAttributes =

if(oppCode == OM_WS.LOGIN) {

return mapping



It is simply converted Objective-C method (what we wrote for mapping in Objective-C) to Swift, in terms of RkEntityMapping as an entity.

[See Also: WatchKit for iOS Apple Watch]

Initialization of RestKit

Now Restkit initialization comes into picture. It is not the same code that we are converting directly into Swift from Objective-C. There are a few differences for initializing RestKit Objects in Objective-C and Swift. To work more efficiently, here we will define RKObjectManager and its mapping with block style. Go to App Delegate and
Declare RKObjectManager and RKManagedObjectStore.

var rkomLogin : RKObjectManager = RKObjectManager()

var rkMos : RKManagedObjectStore =

While Initializing RestKit object and mapping here, we do not directly pass NSManagedObject for mapping as RKEntityMapping, because it gets confusing to get the entity from XCDatamodel and results in CoreData error. As a solution for this problem, I have created separate Mapping Class with return type RKEntityMaping.

func initRestkit() {

// RkManagedObjectStore

var managedObjectModel =


rkMos =



let storePath =


var persistentStore =


fromSeedDatabaseAtPath: nil,

withConfiguration: nil,

options: optionsForSqliteStore(),

error: nil



rkMos.managedObjectCache = RKInMemoryManagedObjectCache(



// RKManaged Object

rkomLogin = RKObjectManager(baseURL:
NSURL(string: kBase_URL))


managedObjectStore = rkMos


var mappingAuth: RKEntityMapping = dataMapping.objectMappingForAuth(OM_WS.LOGIN)

let responseDescriptor = RKResponseDescriptor(

mapping: mappingAuth,

method: RKRequestMethod.POST,

pathPattern: nil,

keyPath: “”,

statusCodes: NSIndexSet(index: 200)




What exactly did we do there? We initialized rkMos, binded it with managedObject, and created ManagedObjectContext. While Defining RkObjectManager here, we do not need a required request descriptor. For instance, initialize a response descriptor in block and add that in RkObjectManager.

Call this method at launching application from AppDelegate:

func application(application: UIApplication,
didFinishLaunchingWithOptions launchOptions:
[NSObject: AnyObject]?) -> Bool {

// Override point for customization after
application launch.


return true}

That is it! You have successfully integrated RestKit. Now how can we execute Request and Get Response as an Object?
We can implement this from any ViewController.

Execute Request from ViewController

Go To ViewController and create a method:

func getLoginReq(){

var appDelegate =

UIApplication.sharedApplication().delegate as!


var dict = [“email”:””, “password”:

appDelegate.rkomLogin.postObject(nil, path:
“auth/local”, parameters: dict, success: {
(operatio: RKObjectRequestOperation!, mapping: RKMappingResult!) -> Void in


// Here we get response in object.

var auth : Authenticate =
mapping.array().first as! Authenticate

}, failure:
-> Void in




With the same way you can create another request for response and mapping, you can call the method wherever it is required. This is how you can start using Swift language in iOS development and make positive steps in working with Apple iOS.

Everything you need to know about outsourcing technology developmentAccess a special Introduction Package with everything you want to know about outsourcing your technology development. How should you evaluate a partner? What components of your solution that are suitable to be handed off to a partner? These answers and more below.

Introduction Package

Source: zymr

Leave a comment

Your email address will not be published.

Let's continue the conversation!

    Please prove you are human by selecting the Plane.