Giter Club home page Giter Club logo

objectmapper_realmswift's Introduction

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

objectmapper_realmswift's People

Contributors

finayev avatar nikkovios avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

objectmapper_realmswift's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.