All Projects → NikKovIos → ObjectMapper_RealmSwift

NikKovIos / ObjectMapper_RealmSwift

Licence: MIT license
Helps to parse RealmSwift's List<Class>() and RealmOptional<T>() properties with ObjectMapper framework.

Programming Languages

ruby
36898 projects - #4 most used programming language
swift
15916 projects

Projects that are alternatives of or similar to ObjectMapper RealmSwift

realms-ios
Safe method for Realm
Stars: ✭ 22 (+29.41%)
Mutual labels:  objectmapper
BetterMappable
Better Mappable through Property Wrappers using ObjectMapper
Stars: ✭ 26 (+52.94%)
Mutual labels:  objectmapper
Perfect-Server-Side-Swift iOS-App
A family tree API server implementation with iOS client. Server has been implemented with Perfect: Server-Side Swift And iOS client is in pure Swift.
Stars: ✭ 15 (-11.76%)
Mutual labels:  objectmapper
jds
Jenesis Data Store: a dynamic, cross platform, high performance, ORM data-mapper. Designed to assist in rapid development and data mining
Stars: ✭ 17 (+0%)
Mutual labels:  objectmapper
DictionaryUtils
iOS Dictionary Utilities for Swift - allowing you to pull properties from a dictionary using a string eg. data.readString("field[0].name")
Stars: ✭ 13 (-23.53%)
Mutual labels:  objectmapper
AlamofireMapper
Mapper for Alamofire use Swift 4 decoable
Stars: ✭ 18 (+5.88%)
Mutual labels:  objectmapper
realm-mapper-ios
Realm + ObjectMapper
Stars: ✭ 28 (+64.71%)
Mutual labels:  objectmapper

ObjectMapper_RealmSwift

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].