Latest 3.0
Homepage https://github.com/zendobk/RealmMapper
License MIT
Platforms ios 8.0, requires ARC
Dependencies RealmSwift, ObjectMapper
Authors

Build Status
CocoaPods Compatible
Platform
Code Coverage

RealmSwift
ObjectMapper

Realm + ObjectMapper

Requirements

  • iOS 8.0+
  • Xcode 8.3 (Swift 3.1)

Installation

Embedded frameworks require a minimum deployment target of iOS 8

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

CocoaPods 1.2+ is required to build RealmMapper 3.0+.

To integrate RealmMapper into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!

pod 'RealmMapper', '~> 3.0'

Then, run the following command:

$ pod install

Usage

Mapping

Rule:

  • Object has primaryKey must be StaticMappable (i)
  • Object has no primaryKey should be Mappable (ii)
import RealmSwift
import ObjectMapper
import RealmMapper

final class User: Object, StaticMappable {
    dynamic var id: String!
    dynamic var name: String?
    dynamic var address: Address?
    let dogs = List<Dog>()

    override class func primaryKey() -> String? {
        return "id"
    }

    func mapping(map: Map) {
        name <- map["name"]
        address <- map["address"]
        dogs <- map["dogs"]
    }

    static func objectForMapping(map: Map) -> BaseMappable? {
        do {
            let realm = try Realm()
            return realm.object(ofType: self, forMapping: map)
        } catch {
            return nil
        }
    }
}

// (ii)
final class Address: Object, Mappable {
    dynamic var street = ""
    dynamic var city = ""
    dynamic var country = ""

    dynamic var phone: Phone?

    let users = LinkingObjects(fromType: User.self, property: "address")

    convenience required init?(map: Map) {
        self.init()
    }

    func mapping(map: Map) {
        street <- map["street"]
        city <- map["city"]
        country <- map["country"]
        phone <- map["phone"]
    }
}

Import JSON to Realm

do {
  let realm = try Realm()
  try realm.write {
    realm.map(User.self, json: jsUser) // map JSON object
    realm.map(Shop.self, json: jsShops) // map JSON array
  }
} catch {
}

nil value will be bypass, if you want set nil please use NSNull() instead

Latest podspec

{
    "name": "RealmMapper",
    "version": "3.0",
    "license": "MIT",
    "summary": "RealmMapper",
    "homepage": "https://github.com/zendobk/RealmMapper",
    "authors": {
        "Dao Nguyen": "zendobk"
    },
    "source": {
        "git": "https://github.com/zendobk/RealmMapper.git",
        "tag": "3.0"
    },
    "requires_arc": true,
    "platforms": {
        "ios": "8.0"
    },
    "ios": {
        "frameworks": [
            "Foundation",
            "UIKit"
        ]
    },
    "dependencies": {
        "RealmSwift": [
            "~> 3.0"
        ],
        "ObjectMapper": [
            "~> 2.2"
        ]
    },
    "source_files": "Sources/*.swift",
    "pod_target_xcconfig": {
        "SWIFT_VERSION": "3.1"
    },
    "pushed_with_swift_version": "3.1"
}

Pin It on Pinterest

Share This