Latest 0.1.1
Homepage https://github.com/NikKovIos/ObjectMapper_RealmSwift
License MIT
Platforms ios 8.0
Dependencies RealmSwift, ObjectMapper
Frameworks UIKit
Authors

Cocoapods
Platform
Version
Swift version
License

Installation

ObjectMapper_RealmSwift is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod 'ObjectMapper_RealmSwift'

Example

Here is the sample code how to use it. Assume you have installed RealmSwift and ObjectMapper. Next step to create realm object.

Json you get

 {
    "id": 12,
    "email": "[email protected]",
    "phone": "+79996663213",
    "firstName": "Mark",
    "lastName": null,
    "privateInfo": {
      "city": {
          "id": 1,
          "value": "Vice City"
        },
      "emails": [
        {
          "id": 22,
          "value": "[email protected]"
        }
      ],
      "country": {
        "id": 1,
          "value": "Calefornication"
        },
      "birthday": 1042070400
    },
    "socialMedia": {
    "messengers": [
      {
        "name": "one",
        "userId": "+79997776666",
        "userName": "+79998887777"
      },
      {
        "name": "two",
        "userId": "3456622",
        "userName": "megaCoolGuy",
        "userPhone": "+79995663322"
      }
    ],
    "socialNetworks": [
      {
        "name": "first",
        "userId": "31515121221",
        "userName": "Yellow Duck"
      },
      {
        "name": "second",
        "userId": "32532523532",
        "userName": "Luckky"
      }
    ],
   "timeOfPhotoUpload": 1485442694,
   "avatarUrl": "http://any_link.com/hot_girl.png",
 }

Here is the model

According to https://realm.io/docs/swift/latest/#cheatsheet don’t forget dynamic and let where it is nessesary.

import RealmSwift
import ObjectMapper
import ObjectMapper_RealmSwift

class UserProfile: Object, Mappable {
  let id = RealmOptional<Int>()
  dynamic var email: String? = nil
  dynamic var phone: String = ""
  dynamic var firstName: String? = nil
  dynamic var lastName: String? = nil
  dynamic var privateInfo: PrivateInfo?
  let socialMedia = List<Messenger>()
  let socialNetworks = List<SocialNetwork>()
  let timeOfPhotoUpload = RealmOptional<Double>()
  dynamic var avatarUrl: String? = nil

  required convenience init?(map: Map) {
    if
        !map["id"].isKeyPresent ||
        !map["phone"].isKeyPresent
    {
      print("Can't parse profile info")
      return nil
    }
    self.init()
  }

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

  func mapping(map: Map) {
    id                <- map["id"]
    email             <- map["email"]
    phone             <- map["phone"]
    firstName         <- map["firstName"]
    lastName          <- map["lastName"]
    privateInfo       <- map["privateInfo"]
    socialMedia       <- map["socialMedia"]
    socialNetworks    <- map["socialNetworks"]
    timeOfPhotoUpload <- (map["privateInfo"], DateTransform())
    avatarUrl         <- map["avatarUrl"]
  }
}

and nested classes:

class Messenger: Object, Mappable {
  dynamic var name: String? = ""
  dynamic var userID: String? = ""
  dynamic var userName: String? = ""

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

  func mapping(map: Map) {
    name            <- map["name"]
    userID          <- map["userId"]
    userName        <- map["userName"]
  }
}
class PrivateInfo: Object, Mappable {
  dynamic var city: Item?
  let emails = List<Item>()
  dynamic var country: Item?
  var birthday = RealmOptional<Double>()

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

  func mapping(map: Map) {
    city              <- map["city"]
    emails            <- map["emails"]
    country           <- map["country"]
    birthday          <- (map["birthday"], DateTransform())
  }
}

Took from

https://gist.github.com/danilValeev/ef29630b61eed510ca135034c444a98a

License

ObjectMapper_RealmSwift is available under the MIT license. See the LICENSE file for more info.

My other Repos

Latest podspec

{
    "name": "ObjectMapper_RealmSwift",
    "version": "0.1.1",
    "summary": "Helps to parse RealmSwift List() and RealmOptional() properties with ObjectMapper framework.",
    "description": "ObjectMapper (https://github.com/Hearst-DD/ObjectMapper) is a great tool to map entities. nBut there are no functional to map directly to realm objects. nObjectMapper_RealmSwift helps to parse List() and RealmOptional() properties with ObjectMapper framework and store them correctly nwithout "Transforms".",
    "homepage": "https://github.com/NikKovIos/ObjectMapper_RealmSwift",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Nik Kov": "[email protected]"
    },
    "source": {
        "git": "https://github.com/NikKovIos/ObjectMapper_RealmSwift.git",
        "tag": "0.1.1"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "ObjectMapper_RealmSwift/Classes/**/*",
    "frameworks": "UIKit",
    "dependencies": {
        "RealmSwift": [
            "~> 2.3"
        ],
        "ObjectMapper": [
            "~> 2.2"
        ]
    },
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This