matthewcheok / Realm-JSON

A concise Mantle-like way of working with Realm and JSON.
MIT License
661 stars 129 forks source link

How to parse an array of string #60

Closed tuantm2014 closed 9 years ago

tuantm2014 commented 9 years ago

Does anyone know how to parse an array of string. Like this:

"zoneInfo": {
    "tariffInfoLines": [
        "In this city you pay per minute."
    ]
}

We have a zoneInfo object that contains a tariffInfoLines array. This tariffInfoLines array contains strings. To store in Realm, i define RealmString object and store it in RLMArray

@interface RealmString : RLMObject
@property NSString *stringValue;
@end
@implementation RealmString
@end

RLM_ARRAY_TYPE(RealmString)

@interface TariffInfo : RLMObject
@property RLMArray<RealmString> *storedTariffInfoLines;
@end

But i dont know how to parse it from JSON using Realm-JSON. The application will crash if you try to. So i really need some help to solve this problem!

matthewcheok commented 9 years ago

That's because you haven't defined the mapping for stringValue:

@interface RealmString : RLMObject
@property NSString *stringValue;
@end
@implementation RealmString
+ (NSDictionary *)JSONInboundMappingDictionary {
  return @{
         @"self": @"stringValue",
  };
}
@end
tuantm2014 commented 9 years ago

Thanks @matthewcheok but it seems not work. It still crash at line 190 of RLMObject+JSON.m

for (id item in(NSArray*) value) {
    [array addObject:[elementClass mc_createObjectFromJSONDictionary:item]];
}

Because item is type of NSString not NSDictionary

matthewcheok commented 9 years ago

Your issue looks very similar to matthewcheok/Realm-JSON#4 Mind sharing your code sample?

tuantm2014 commented 9 years ago

Oh, it works now after clean database and project. Thanks so much @matthewcheok