2016-06-30 53 views
7

Nel mio modello di oggetti Realm ho un oggetto chiamato "Event". Ogni evento ha un elenco di EventLocatons. Sto cercando di mappare questi oggetti da JSON, ma l'elenco di EventLocations è sempre vuoto. Gli oggetti simile a questa (semplificato per chiarezza):Come mappare un elenco di reame di oggetti personalizzati usando il protocollo Mappable in Swift

class Event: Object, Mappable { 
    override class func primaryKey() -> String? { 
     return "id" 
    } 

    dynamic var id = "" 
    var eventLocations:List<EventLocation> = List<EventLocation>() 

    func mapping(map: Map) { 
     id <- map["id"] 
     eventLocations <- map["eventLocations"] 
    } 
} 

class EventLocation: Object, Mappable { 
    override class func primaryKey() -> String? { 
     return "id" 
    } 

    dynamic var id: String = "" 
    dynamic var name: String = "" 

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

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

JSON, che ho è un array di oggetti evento. Proviene da una risposta Alamofire e ho la mappa in quel modo:

var events = Mapper<Event>().mapArray(json!) 

JSON è simile al seguente:

[ 
    { 
    "id" : "21dedd6d", 
    "eventLocations" : [ 
     { 
     "name" : "hh", 
     "id" : "e18df48a", 
     }, 
     { 
     "name" : "tt", 
     "fileId" : "be6116e", 
     } 
    ] 
    }, 
    { 
    "id" : "e694c885", 
    "eventLocations" : [ 
     { 
     "name" : "hh", 
     "id" : "e18df48a", 
     }, 
     { 
     "name" : "tt", 
     "fileId" : "be6116e", 
     } 
    ] 
    } 
] 

Qualcuno sa come posso mappare un elenco di oggetti personalizzati utilizzando il protocollo mappabili. Whay è la lista "eventLocations" sempre vuota?

risposta

8

Avendo uno sguardo su one of the Issues page on ObjectMapper's GitHub repo, non sembra che gli oggetti di Realm List siano ancora supportati correttamente.

Tale questione elenca anche una potenziale soluzione per farla funzionare per il momento, che mi rispecchio qui:

class MyObject: Object, Mappable { 
    let tags = List<Tag>() 

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

    func mapping(map: Map) { 
     var tags: [Tag]? 
     tags <- map["tags"] 
     if let tags = tags { 
      for tag in tags { 
       self.tags.append(tag) 
      } 
     } 
    } 
} 
+0

Questa è la migliore soluzione, il regno funziona con gestito, quindi non può dichiarare lista di tipo var, usa sempre let! Grazie! –

2

Un'altra soluzione potrebbe essere quella di implementare una trasformazione personalizzata per ObjectMapper. È possibile trovare un'implementazione here.

Poi nel codice:

eventLocations <- (map["eventLocations"], ListTransform<EventLocation>()) 
1

È possibile aggiungere un operatore per questo.

Swift 3 applicazione:

import Foundation 
import RealmSwift 
import ObjectMapper 

infix operator <- 

/// Object of Realm's List type 
public 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) 
     } 
    } 
} 

Ora non è necessario alcun codice aggiuntivo o trasformazioni.

list <- map["name"]

Ho creato un Gist. Per favore, controlla https://gist.github.com/danilValeev/ef29630b61eed510ca135034c444a98a