2014-09-30 13 views
7

Come posso utilizzare Github Mantle per scegliere una classe di proprietà basata su un'altra proprietà nella stessa classe? (o nel caso peggiore un'altra parte dell'oggetto JSON).Classe di proprietà Mantle basata su un'altra proprietà?

Per esempio se ho un oggetto come questo:

{ 
    "content": {"mention_text": "some text"}, 
    "created_at": 1411750819000, 
    "id": 600, 
    "type": "mention" 
} 

Voglio fare un trasformatore come questo:

+(NSValueTransformer *)contentJSONTransformer { 
    return [MTLValueTransformer transformerWithBlock:^id(NSDictionary* contentDict) { 
      return [MTLJSONAdapter modelOfClass:ETMentionActivityContent.class fromJSONDictionary:contentDict error:nil]; 
    }]; 
} 

Ma il dizionario passato al trasformatore include solo il 'contenuto' parte del JSON, quindi non ho accesso al campo 'tipo'. Esiste comunque l'accesso al resto dell'oggetto? O in qualche modo basare la classe del modello di 'contenuto' sul 'tipo'?

ho già stato costretto a fare le soluzioni di hack come questo:

+(NSValueTransformer *)contentJSONTransformer { 
    return [MTLValueTransformer transformerWithBlock:^id(NSDictionary* contentDict) { 
     if (contentDict[@"mention_text"]) { 
      return [MTLJSONAdapter modelOfClass:ETMentionActivityContent.class fromJSONDictionary:contentDict error:nil]; 
     } else { 
      return [MTLJSONAdapter modelOfClass:ETActivityContent.class fromJSONDictionary:contentDict error:nil]; 
     } 
    }]; 
} 

risposta

0

Ho avuto un problema simile, e ho il sospetto la mia soluzione non è molto migliore della tua.

Ho una classe base comune per i miei oggetti Mantle, e dopo che ciascuno è stato creato, chiamo un metodo configure per dare loro la possibilità di impostare proprietà che dipendono da più di una proprietà "base" (== JSON) .

Ti piace questa:

+(id)entityWithDictionary:(NSDictionary*)dictionary { 

    NSError* error = nil; 
    Class derivedClass = [self classWithDictionary:dictionary]; 
    NSAssert(derivedClass,@"entityWithDictionary failed to make derivedClass"); 
    HVEntity* entity = [MTLJSONAdapter modelOfClass:derivedClass fromJSONDictionary:dictionary error:&error]; 
    NSAssert(entity,@"entityWithDictionary failed to make object"); 
    entity.raw = dictionary; 
    [entity configureWithDictionary:dictionary]; // Give the new entity a chance to set up derived properties 
    return entity; 
} 
5

È possibile passare le informazioni sul tipo modificando il metodo JSONKeyPathsByPropertyKey:

+ (NSDictionary *)JSONKeyPathsByPropertyKey 
{ 
    return @{ 
     NSStringFromSelector(@selector(content)) : @[ @"type", @"content" ], 
    }; 
} 

Poi, nel contentJSONTransformer, è possibile accedere al "tipo" di proprietà:

+ (NSValueTransformer *)contentJSONTransformer 
{ 
    return [MTLValueTransformer ... 
     ... 
     NSString *type = value[@"type"]; 
     id content = value[@"content"]; 
    ]; 
} 
+0

È la soluzione perfetta! Grazie. Molti problemi hanno risolto. – CFIFok

Problemi correlati