Alamofire, Objectmapper, Realm: Nested Objects









up vote
4
down vote

favorite
1












I'm using Alamofire, Objectmapper, Realm and everything is working beside one thing: I can't map nested objects.



class Voting: Object, Mappable 

dynamic var votingID: String = ""
dynamic var question: String = ""
var votingOptions = List<VotingOption>()

required convenience init?(_ map: Map)
self.init()


func mapping(map: Map)
votingID <- map["id"]
question <- map["question"]
votingOptions <- map["votingOptions"]


override class func primaryKey() -> String
return "votingID"



class VotingOption: Object, Mappable

dynamic var optionID: String = ""
dynamic var text: String = ""


required convenience init?(_ map: Map)
self.init()


func mapping(map: Map)
optionID <- map["id"]
text <- map["optionText"]


override class func primaryKey() -> String
return "optionID"




The JSON that I'm trying to map is:




"Voting": [

"question": "Which option do yo prefer?",
"id": "7f073efd-6f3d-43f2-9fe4-5cad683b77a2",
"votingOptions": [

"optionText": "Option 3",
"id": "3bc0a618-8791-4862-a7fd-5f2df464697d"
,

"optionText": "Option 1",
"id": "84c6a830-814b-40c8-a252-c074be5d689a"
,

"optionText": "Option 2",
"id": "8872ef6f-fc70-445a-802e-d39944006467"

]

]



The mapping funktion in VotingOption never gets called.










share|improve this question

























    up vote
    4
    down vote

    favorite
    1












    I'm using Alamofire, Objectmapper, Realm and everything is working beside one thing: I can't map nested objects.



    class Voting: Object, Mappable 

    dynamic var votingID: String = ""
    dynamic var question: String = ""
    var votingOptions = List<VotingOption>()

    required convenience init?(_ map: Map)
    self.init()


    func mapping(map: Map)
    votingID <- map["id"]
    question <- map["question"]
    votingOptions <- map["votingOptions"]


    override class func primaryKey() -> String
    return "votingID"



    class VotingOption: Object, Mappable

    dynamic var optionID: String = ""
    dynamic var text: String = ""


    required convenience init?(_ map: Map)
    self.init()


    func mapping(map: Map)
    optionID <- map["id"]
    text <- map["optionText"]


    override class func primaryKey() -> String
    return "optionID"




    The JSON that I'm trying to map is:




    "Voting": [

    "question": "Which option do yo prefer?",
    "id": "7f073efd-6f3d-43f2-9fe4-5cad683b77a2",
    "votingOptions": [

    "optionText": "Option 3",
    "id": "3bc0a618-8791-4862-a7fd-5f2df464697d"
    ,

    "optionText": "Option 1",
    "id": "84c6a830-814b-40c8-a252-c074be5d689a"
    ,

    "optionText": "Option 2",
    "id": "8872ef6f-fc70-445a-802e-d39944006467"

    ]

    ]



    The mapping funktion in VotingOption never gets called.










    share|improve this question























      up vote
      4
      down vote

      favorite
      1









      up vote
      4
      down vote

      favorite
      1






      1





      I'm using Alamofire, Objectmapper, Realm and everything is working beside one thing: I can't map nested objects.



      class Voting: Object, Mappable 

      dynamic var votingID: String = ""
      dynamic var question: String = ""
      var votingOptions = List<VotingOption>()

      required convenience init?(_ map: Map)
      self.init()


      func mapping(map: Map)
      votingID <- map["id"]
      question <- map["question"]
      votingOptions <- map["votingOptions"]


      override class func primaryKey() -> String
      return "votingID"



      class VotingOption: Object, Mappable

      dynamic var optionID: String = ""
      dynamic var text: String = ""


      required convenience init?(_ map: Map)
      self.init()


      func mapping(map: Map)
      optionID <- map["id"]
      text <- map["optionText"]


      override class func primaryKey() -> String
      return "optionID"




      The JSON that I'm trying to map is:




      "Voting": [

      "question": "Which option do yo prefer?",
      "id": "7f073efd-6f3d-43f2-9fe4-5cad683b77a2",
      "votingOptions": [

      "optionText": "Option 3",
      "id": "3bc0a618-8791-4862-a7fd-5f2df464697d"
      ,

      "optionText": "Option 1",
      "id": "84c6a830-814b-40c8-a252-c074be5d689a"
      ,

      "optionText": "Option 2",
      "id": "8872ef6f-fc70-445a-802e-d39944006467"

      ]

      ]



      The mapping funktion in VotingOption never gets called.










      share|improve this question













      I'm using Alamofire, Objectmapper, Realm and everything is working beside one thing: I can't map nested objects.



      class Voting: Object, Mappable 

      dynamic var votingID: String = ""
      dynamic var question: String = ""
      var votingOptions = List<VotingOption>()

      required convenience init?(_ map: Map)
      self.init()


      func mapping(map: Map)
      votingID <- map["id"]
      question <- map["question"]
      votingOptions <- map["votingOptions"]


      override class func primaryKey() -> String
      return "votingID"



      class VotingOption: Object, Mappable

      dynamic var optionID: String = ""
      dynamic var text: String = ""


      required convenience init?(_ map: Map)
      self.init()


      func mapping(map: Map)
      optionID <- map["id"]
      text <- map["optionText"]


      override class func primaryKey() -> String
      return "optionID"




      The JSON that I'm trying to map is:




      "Voting": [

      "question": "Which option do yo prefer?",
      "id": "7f073efd-6f3d-43f2-9fe4-5cad683b77a2",
      "votingOptions": [

      "optionText": "Option 3",
      "id": "3bc0a618-8791-4862-a7fd-5f2df464697d"
      ,

      "optionText": "Option 1",
      "id": "84c6a830-814b-40c8-a252-c074be5d689a"
      ,

      "optionText": "Option 2",
      "id": "8872ef6f-fc70-445a-802e-d39944006467"

      ]

      ]



      The mapping funktion in VotingOption never gets called.







      swift realm alamofire objectmapper






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 19 '15 at 12:37









      netshark1000

      3,47543867




      3,47543867






















          4 Answers
          4






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          The old ListTransform solution no longer works in Swift 3.



          This is what I'm using now; put this in a file called, ListExtensions.swift, for example.



          import Foundation
          import ObjectMapper
          import RealmSwift

          /// Maps object of Realm's List type
          func <- <T: Mappable>(left: List<T>, right: Map)

          var array: [T]?

          if right.mappingType == .toJSON
          array = Array(left)


          array <- right

          if right.mappingType == .fromJSON
          if let theArray = array
          left.append(objectsIn: theArray)





          This allows you to simply use it like this:



          class Parent: Object, Mappable 
          dynamic var id: Int = 0
          var children = List<Child>()

          required convenience init?(_ map: Map)
          self.init()


          func mapping(map: Map)
          id <- map["id"]
          children <- map["children"]







          share|improve this answer


















          • 1




            Faced with this problem too. Your solution helped! You are a lifesaver, thanks!
            – Hot'n'Young
            Nov 9 at 15:30

















          up vote
          3
          down vote













          The problem you're seeing is due to ObjectMapper having no knowledge of Realm's List type. It is not aware that it is a collection type, and that it must be mutated in place rather than being assigned to. You can see discussion of this, including some suggested workarounds, in ObjectMapper GitHub issue #143.



          Note also that any List properties on Object subclasses should be declared with let rather than var.






          share|improve this answer



























            up vote
            3
            down vote













            class ListTransform<T:RealmSwift.Object> : TransformType where T:Mappable 
            typealias Object = List<T>
            typealias JSON = [AnyObject]

            let mapper = Mapper<T>()

            func transformFromJSON(_ value: Any?) -> Object?
            let results = List<T>()
            if let objects = mapper.mapArray(JSONObject: value)
            for object in objects
            results.append(object)


            return results


            func transformToJSON(_ value: Object?) -> JSON?
            var results = [AnyObject]()
            if let value = value
            for obj in value
            let json = mapper.toJSON(obj)
            results.append(json as AnyObject)


            return results




            Then in your model something like this.



            class Parent: Object, Mappable 
            dynamic var id: Int = 0
            var children = List<Child>()

            required convenience init?(_ map: Map)
            self.init()


            func mapping(map: Map)
            id <- map["id"]
            child <- (map["children"], ListTransform<Child>())







            share|improve this answer






















            • thanks, I have reused your code for parsing dict into array of objects where object has a field that contains the key in the dict for that object.
              – Anton Tropashko
              Jun 27 '16 at 10:39










            • This no longer works in Swift 3.
              – Colin Basnett
              Feb 21 '17 at 0:49










            • Try the current version
              – GregP
              Feb 21 '17 at 15:42

















            up vote
            -1
            down vote













            You can extend ObjectMapper for Realm.List type with a operator function like as:



            public func <- <T: Object where T: Mappable, T: JSPrimaryKey>(left: List<T>, right: Map) 
            if right.mappingType == MappingType.FromJSON
            if let value = right.currentValue
            left.removeAll()
            if let json = value as? [[String : AnyObject]]
            let objs = RealmS().add(T.self, json: json)
            left.appendContentsOf(objs)






            Try yourself.



            ObjectMappper + Realm List type






            share|improve this answer






















              Your Answer






              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "1"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













               

              draft saved


              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f33804181%2falamofire-objectmapper-realm-nested-objects%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              4
              down vote



              accepted










              The old ListTransform solution no longer works in Swift 3.



              This is what I'm using now; put this in a file called, ListExtensions.swift, for example.



              import Foundation
              import ObjectMapper
              import RealmSwift

              /// Maps object of Realm's List type
              func <- <T: Mappable>(left: List<T>, right: Map)

              var array: [T]?

              if right.mappingType == .toJSON
              array = Array(left)


              array <- right

              if right.mappingType == .fromJSON
              if let theArray = array
              left.append(objectsIn: theArray)





              This allows you to simply use it like this:



              class Parent: Object, Mappable 
              dynamic var id: Int = 0
              var children = List<Child>()

              required convenience init?(_ map: Map)
              self.init()


              func mapping(map: Map)
              id <- map["id"]
              children <- map["children"]







              share|improve this answer


















              • 1




                Faced with this problem too. Your solution helped! You are a lifesaver, thanks!
                – Hot'n'Young
                Nov 9 at 15:30














              up vote
              4
              down vote



              accepted










              The old ListTransform solution no longer works in Swift 3.



              This is what I'm using now; put this in a file called, ListExtensions.swift, for example.



              import Foundation
              import ObjectMapper
              import RealmSwift

              /// Maps object of Realm's List type
              func <- <T: Mappable>(left: List<T>, right: Map)

              var array: [T]?

              if right.mappingType == .toJSON
              array = Array(left)


              array <- right

              if right.mappingType == .fromJSON
              if let theArray = array
              left.append(objectsIn: theArray)





              This allows you to simply use it like this:



              class Parent: Object, Mappable 
              dynamic var id: Int = 0
              var children = List<Child>()

              required convenience init?(_ map: Map)
              self.init()


              func mapping(map: Map)
              id <- map["id"]
              children <- map["children"]







              share|improve this answer


















              • 1




                Faced with this problem too. Your solution helped! You are a lifesaver, thanks!
                – Hot'n'Young
                Nov 9 at 15:30












              up vote
              4
              down vote



              accepted







              up vote
              4
              down vote



              accepted






              The old ListTransform solution no longer works in Swift 3.



              This is what I'm using now; put this in a file called, ListExtensions.swift, for example.



              import Foundation
              import ObjectMapper
              import RealmSwift

              /// Maps object of Realm's List type
              func <- <T: Mappable>(left: List<T>, right: Map)

              var array: [T]?

              if right.mappingType == .toJSON
              array = Array(left)


              array <- right

              if right.mappingType == .fromJSON
              if let theArray = array
              left.append(objectsIn: theArray)





              This allows you to simply use it like this:



              class Parent: Object, Mappable 
              dynamic var id: Int = 0
              var children = List<Child>()

              required convenience init?(_ map: Map)
              self.init()


              func mapping(map: Map)
              id <- map["id"]
              children <- map["children"]







              share|improve this answer














              The old ListTransform solution no longer works in Swift 3.



              This is what I'm using now; put this in a file called, ListExtensions.swift, for example.



              import Foundation
              import ObjectMapper
              import RealmSwift

              /// Maps object of Realm's List type
              func <- <T: Mappable>(left: List<T>, right: Map)

              var array: [T]?

              if right.mappingType == .toJSON
              array = Array(left)


              array <- right

              if right.mappingType == .fromJSON
              if let theArray = array
              left.append(objectsIn: theArray)





              This allows you to simply use it like this:



              class Parent: Object, Mappable 
              dynamic var id: Int = 0
              var children = List<Child>()

              required convenience init?(_ map: Map)
              self.init()


              func mapping(map: Map)
              id <- map["id"]
              children <- map["children"]








              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 9 at 19:39

























              answered Feb 21 '17 at 0:51









              Colin Basnett

              2,7421936




              2,7421936







              • 1




                Faced with this problem too. Your solution helped! You are a lifesaver, thanks!
                – Hot'n'Young
                Nov 9 at 15:30












              • 1




                Faced with this problem too. Your solution helped! You are a lifesaver, thanks!
                – Hot'n'Young
                Nov 9 at 15:30







              1




              1




              Faced with this problem too. Your solution helped! You are a lifesaver, thanks!
              – Hot'n'Young
              Nov 9 at 15:30




              Faced with this problem too. Your solution helped! You are a lifesaver, thanks!
              – Hot'n'Young
              Nov 9 at 15:30












              up vote
              3
              down vote













              The problem you're seeing is due to ObjectMapper having no knowledge of Realm's List type. It is not aware that it is a collection type, and that it must be mutated in place rather than being assigned to. You can see discussion of this, including some suggested workarounds, in ObjectMapper GitHub issue #143.



              Note also that any List properties on Object subclasses should be declared with let rather than var.






              share|improve this answer
























                up vote
                3
                down vote













                The problem you're seeing is due to ObjectMapper having no knowledge of Realm's List type. It is not aware that it is a collection type, and that it must be mutated in place rather than being assigned to. You can see discussion of this, including some suggested workarounds, in ObjectMapper GitHub issue #143.



                Note also that any List properties on Object subclasses should be declared with let rather than var.






                share|improve this answer






















                  up vote
                  3
                  down vote










                  up vote
                  3
                  down vote









                  The problem you're seeing is due to ObjectMapper having no knowledge of Realm's List type. It is not aware that it is a collection type, and that it must be mutated in place rather than being assigned to. You can see discussion of this, including some suggested workarounds, in ObjectMapper GitHub issue #143.



                  Note also that any List properties on Object subclasses should be declared with let rather than var.






                  share|improve this answer












                  The problem you're seeing is due to ObjectMapper having no knowledge of Realm's List type. It is not aware that it is a collection type, and that it must be mutated in place rather than being assigned to. You can see discussion of this, including some suggested workarounds, in ObjectMapper GitHub issue #143.



                  Note also that any List properties on Object subclasses should be declared with let rather than var.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 19 '15 at 22:59









                  bdash

                  14.9k13777




                  14.9k13777




















                      up vote
                      3
                      down vote













                      class ListTransform<T:RealmSwift.Object> : TransformType where T:Mappable 
                      typealias Object = List<T>
                      typealias JSON = [AnyObject]

                      let mapper = Mapper<T>()

                      func transformFromJSON(_ value: Any?) -> Object?
                      let results = List<T>()
                      if let objects = mapper.mapArray(JSONObject: value)
                      for object in objects
                      results.append(object)


                      return results


                      func transformToJSON(_ value: Object?) -> JSON?
                      var results = [AnyObject]()
                      if let value = value
                      for obj in value
                      let json = mapper.toJSON(obj)
                      results.append(json as AnyObject)


                      return results




                      Then in your model something like this.



                      class Parent: Object, Mappable 
                      dynamic var id: Int = 0
                      var children = List<Child>()

                      required convenience init?(_ map: Map)
                      self.init()


                      func mapping(map: Map)
                      id <- map["id"]
                      child <- (map["children"], ListTransform<Child>())







                      share|improve this answer






















                      • thanks, I have reused your code for parsing dict into array of objects where object has a field that contains the key in the dict for that object.
                        – Anton Tropashko
                        Jun 27 '16 at 10:39










                      • This no longer works in Swift 3.
                        – Colin Basnett
                        Feb 21 '17 at 0:49










                      • Try the current version
                        – GregP
                        Feb 21 '17 at 15:42














                      up vote
                      3
                      down vote













                      class ListTransform<T:RealmSwift.Object> : TransformType where T:Mappable 
                      typealias Object = List<T>
                      typealias JSON = [AnyObject]

                      let mapper = Mapper<T>()

                      func transformFromJSON(_ value: Any?) -> Object?
                      let results = List<T>()
                      if let objects = mapper.mapArray(JSONObject: value)
                      for object in objects
                      results.append(object)


                      return results


                      func transformToJSON(_ value: Object?) -> JSON?
                      var results = [AnyObject]()
                      if let value = value
                      for obj in value
                      let json = mapper.toJSON(obj)
                      results.append(json as AnyObject)


                      return results




                      Then in your model something like this.



                      class Parent: Object, Mappable 
                      dynamic var id: Int = 0
                      var children = List<Child>()

                      required convenience init?(_ map: Map)
                      self.init()


                      func mapping(map: Map)
                      id <- map["id"]
                      child <- (map["children"], ListTransform<Child>())







                      share|improve this answer






















                      • thanks, I have reused your code for parsing dict into array of objects where object has a field that contains the key in the dict for that object.
                        – Anton Tropashko
                        Jun 27 '16 at 10:39










                      • This no longer works in Swift 3.
                        – Colin Basnett
                        Feb 21 '17 at 0:49










                      • Try the current version
                        – GregP
                        Feb 21 '17 at 15:42












                      up vote
                      3
                      down vote










                      up vote
                      3
                      down vote









                      class ListTransform<T:RealmSwift.Object> : TransformType where T:Mappable 
                      typealias Object = List<T>
                      typealias JSON = [AnyObject]

                      let mapper = Mapper<T>()

                      func transformFromJSON(_ value: Any?) -> Object?
                      let results = List<T>()
                      if let objects = mapper.mapArray(JSONObject: value)
                      for object in objects
                      results.append(object)


                      return results


                      func transformToJSON(_ value: Object?) -> JSON?
                      var results = [AnyObject]()
                      if let value = value
                      for obj in value
                      let json = mapper.toJSON(obj)
                      results.append(json as AnyObject)


                      return results




                      Then in your model something like this.



                      class Parent: Object, Mappable 
                      dynamic var id: Int = 0
                      var children = List<Child>()

                      required convenience init?(_ map: Map)
                      self.init()


                      func mapping(map: Map)
                      id <- map["id"]
                      child <- (map["children"], ListTransform<Child>())







                      share|improve this answer














                      class ListTransform<T:RealmSwift.Object> : TransformType where T:Mappable 
                      typealias Object = List<T>
                      typealias JSON = [AnyObject]

                      let mapper = Mapper<T>()

                      func transformFromJSON(_ value: Any?) -> Object?
                      let results = List<T>()
                      if let objects = mapper.mapArray(JSONObject: value)
                      for object in objects
                      results.append(object)


                      return results


                      func transformToJSON(_ value: Object?) -> JSON?
                      var results = [AnyObject]()
                      if let value = value
                      for obj in value
                      let json = mapper.toJSON(obj)
                      results.append(json as AnyObject)


                      return results




                      Then in your model something like this.



                      class Parent: Object, Mappable 
                      dynamic var id: Int = 0
                      var children = List<Child>()

                      required convenience init?(_ map: Map)
                      self.init()


                      func mapping(map: Map)
                      id <- map["id"]
                      child <- (map["children"], ListTransform<Child>())








                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Feb 21 '17 at 15:41

























                      answered Apr 13 '16 at 20:29









                      GregP

                      1,0361114




                      1,0361114











                      • thanks, I have reused your code for parsing dict into array of objects where object has a field that contains the key in the dict for that object.
                        – Anton Tropashko
                        Jun 27 '16 at 10:39










                      • This no longer works in Swift 3.
                        – Colin Basnett
                        Feb 21 '17 at 0:49










                      • Try the current version
                        – GregP
                        Feb 21 '17 at 15:42
















                      • thanks, I have reused your code for parsing dict into array of objects where object has a field that contains the key in the dict for that object.
                        – Anton Tropashko
                        Jun 27 '16 at 10:39










                      • This no longer works in Swift 3.
                        – Colin Basnett
                        Feb 21 '17 at 0:49










                      • Try the current version
                        – GregP
                        Feb 21 '17 at 15:42















                      thanks, I have reused your code for parsing dict into array of objects where object has a field that contains the key in the dict for that object.
                      – Anton Tropashko
                      Jun 27 '16 at 10:39




                      thanks, I have reused your code for parsing dict into array of objects where object has a field that contains the key in the dict for that object.
                      – Anton Tropashko
                      Jun 27 '16 at 10:39












                      This no longer works in Swift 3.
                      – Colin Basnett
                      Feb 21 '17 at 0:49




                      This no longer works in Swift 3.
                      – Colin Basnett
                      Feb 21 '17 at 0:49












                      Try the current version
                      – GregP
                      Feb 21 '17 at 15:42




                      Try the current version
                      – GregP
                      Feb 21 '17 at 15:42










                      up vote
                      -1
                      down vote













                      You can extend ObjectMapper for Realm.List type with a operator function like as:



                      public func <- <T: Object where T: Mappable, T: JSPrimaryKey>(left: List<T>, right: Map) 
                      if right.mappingType == MappingType.FromJSON
                      if let value = right.currentValue
                      left.removeAll()
                      if let json = value as? [[String : AnyObject]]
                      let objs = RealmS().add(T.self, json: json)
                      left.appendContentsOf(objs)






                      Try yourself.



                      ObjectMappper + Realm List type






                      share|improve this answer


























                        up vote
                        -1
                        down vote













                        You can extend ObjectMapper for Realm.List type with a operator function like as:



                        public func <- <T: Object where T: Mappable, T: JSPrimaryKey>(left: List<T>, right: Map) 
                        if right.mappingType == MappingType.FromJSON
                        if let value = right.currentValue
                        left.removeAll()
                        if let json = value as? [[String : AnyObject]]
                        let objs = RealmS().add(T.self, json: json)
                        left.appendContentsOf(objs)






                        Try yourself.



                        ObjectMappper + Realm List type






                        share|improve this answer
























                          up vote
                          -1
                          down vote










                          up vote
                          -1
                          down vote









                          You can extend ObjectMapper for Realm.List type with a operator function like as:



                          public func <- <T: Object where T: Mappable, T: JSPrimaryKey>(left: List<T>, right: Map) 
                          if right.mappingType == MappingType.FromJSON
                          if let value = right.currentValue
                          left.removeAll()
                          if let json = value as? [[String : AnyObject]]
                          let objs = RealmS().add(T.self, json: json)
                          left.appendContentsOf(objs)






                          Try yourself.



                          ObjectMappper + Realm List type






                          share|improve this answer














                          You can extend ObjectMapper for Realm.List type with a operator function like as:



                          public func <- <T: Object where T: Mappable, T: JSPrimaryKey>(left: List<T>, right: Map) 
                          if right.mappingType == MappingType.FromJSON
                          if let value = right.currentValue
                          left.removeAll()
                          if let json = value as? [[String : AnyObject]]
                          let objs = RealmS().add(T.self, json: json)
                          left.appendContentsOf(objs)






                          Try yourself.



                          ObjectMappper + Realm List type







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Jan 16 '16 at 15:55

























                          answered Jan 16 '16 at 15:31









                          Dao

                          293411




                          293411



























                               

                              draft saved


                              draft discarded















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f33804181%2falamofire-objectmapper-realm-nested-objects%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Use pre created SQLite database for Android project in kotlin

                              Darth Vader #20

                              Ondo