gpt4 book ai didi

ios - RestKit 映射没有键路径的字符串数组

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:02:45 27 4
gpt4 key购买 nike

我在使用 RestKit 0.20.3 映射没有键路径的 JSON 字符串数组时遇到问题。我的类(class)基于 RestKit 中的示例 wiki .

JSON:

"feature_list":{
"property":["test","test ","test","test"],
"street":["test1","foo","bar"],
"garden":["foo","bar"],
"other":["foo","bar", "test2"]
}

类:

@interface FeatureList : NSManagedObject

@property(nonatomic, strong) NSSet *property;
@property(nonatomic, strong) NSSet *street;
@property(nonatomic, strong) NSSet *garden;
@property(nonatomic, strong) NSSet *other;

@end

@interface Feature : NSManagedObject

@property(nonatomic, strong) NSString *featureType;

@end

映射设置:

+ (RKMapping *)featureListMapping {
RKObjectMapping *mapping = [RKEntityMapping mappingForEntityForName:@"FeatureList" inManagedObjectStore:objectStore];
[mapping addRelationshipMappingWithSourceKeyPath:@"property" mapping:[self featureMapping]];
[mapping addRelationshipMappingWithSourceKeyPath:@"street" mapping:[self featureMapping]];
[mapping addRelationshipMappingWithSourceKeyPath:@"garden" mapping:[self featureMapping]];
[mapping addRelationshipMappingWithSourceKeyPath:@"other" mapping:[self featureMapping]];
}

+ (RKMapping *)featureMapping {
RKObjectMapping *mapping = [RKEntityMapping mappingForEntityForName:@"Feature" inManagedObjectStore:objectStore];
[mapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"featureType"]];
return mapping;
}

在 Xcode 中调试 feature.featureType 对象时,返回一个 RKMappingSourceObject 对象,其值存储在我无法访问的对象属性中。打印 feature.featureType.class 时,打印的是 NSCFString。

for (Feature * feature in featureList.property)
{
//feature.featureType is a RKMappingSourceObject in the debugger
NSString *featureStr = feature.featureType.class; //prints NSCFString
}

日志输出:

Mapped attribute value from keyPath '(null)' to 'featureType'. Value: test ({
HTTP = {
request = {
URL = "http://localhost:3000/api/v1/test";
headers = {
};
method = GET;
};
response = {
URL = "http://localhost:3000/api/v1/test";
headers = {
"Cache-Control" = "max-age=0, private, must-revalidate";
"Content-Type" = "application/json; charset=utf-8";
Etag = "\"193d0f470155872e5b36e9f7586c0c8f\"";
"Proxy-Connection" = Close;
Server = "thin 1.5.0 codename Knife";
"X-Request-Id" = 0e4de78e656ef2165699385695cdfe75;
"X-Runtime" = "0.092788";
"X-UA-Compatible" = "IE=Edge";
};
};
};
mapping = {
collectionIndex = 1;
rootKeyPath = response;
};
})

如有任何建议,我们将不胜感激

最佳答案

基本上,您可以对返回的代理对象使用除description 之外的任何方法。这意味着不将其作为变量提供给 NSLog

真正的对象将被存储到数据存储中。根据您需要执行的操作,您可能想要直接检索它们(通过获取或使用托管对象 ID)。

关于ios - RestKit 映射没有键路径的字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19751543/

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