1 karma · joined Jul 21, 2014 (12y ago) · 2 submissions
comment 12y ago · link · parent
It's extra work, but it lets me manipulate Realm into doing what I want it to do to store data which doesn't conform to an explicit schema. (e.g. a Venue which has an array of address strings, where I don't want the headache of iterating through venue.address[i].string values:
(note: this doesn't look pretty, its doing some funky formatting!)
"venue" : { "name":"Test Venue", "address" : [ "address line 1", "address line 2", "address line 3 for rare instances" ] }
@interface Venue : RLMObject @property NSString name; @property NSData addressData; @end
@interface Venue (AddressToData) @property NSArray address; @end
+ (NSDictionary
)defaultPropertyValues { return @{ @"addressData":[NSJSONSerialization dataWithJSONObject:@[] options:0 error:nil] }; }- (NSArray) address { NSArray address = nil;
if (self.addressData) {
address = [NSJSONSerialization JSONObjectWithData:self.addressData options:0 error:nil];
} else {
address = @[];
}
return address;
}- (void) setAddress:(NSArray )address { if (!address) { address = @[]; }
NSData *data = [NSJSONSerialization dataWithJSONObject:address options:0 error:nil];
self.addressData = data;
}