怎么把RLMObject
转换成NSDictionary
?
这是我的代码:
NSString *imei = [Utils getUUID];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"imei = %@",imei];
RLMResults<RLMRequestHeaderModel *> *models = [RLMRequestHeaderModel objectsWithPredicate:pred];
RLMRequestHeaderModel *header = models.firstObject;
// NSDictionary *headerDict = ...
return headerDict;
最佳答案
我已经使用此类解决了这个问题。与先前的答案非常相似,但是在这种情况下,我为RLMArray对象或内部RLMObjects添加了特殊处理
@implementation RLMObject (NSDictionary)
- (NSDictionary*) dictionaryRepresentation{
NSMutableDictionary *headerDictionary = [NSMutableDictionary dictionary];
RLMObjectSchema *schema = self.objectSchema;
for (RLMProperty *property in schema.properties) {
if([self[property.name] isKindOfClass:[RLMArray class]]){
NSMutableArray *arrayObjects = [[NSMutableArray alloc] init];
RLMArray *currentArray = self[property.name];
NSInteger numElements = [currentArray count];
for(int i = 0; i<numElements; i++){
[arrayObjects addObject:[[currentArray objectAtIndex:i] dictionaryRepresentation]];
}
headerDictionary[property.name] = arrayObjects;
}else if([self[property.name] isKindOfClass:[RLMObject class]]){
RLMObject *currentElement = self[property.name];
headerDictionary[property.name] = [currentElement dictionaryRepresentation];
}else{
headerDictionary[property.name] = self[property.name];
}
}
return headerDictionary;
}
@end
让我知道这是否可以帮助您;)