gpt4 book ai didi

ios - iOS OC,如何将RLMObject转换为NSDictionary,NSArray?

转载 作者:行者123 更新时间:2023-12-01 17:29:20 49 4
gpt4 key购买 nike

怎么把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

让我知道这是否可以帮助您;)

关于ios - iOS OC,如何将RLMObject转换为NSDictionary,NSArray?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38247882/

49 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com