3

我在使用 RestKit 0.20.3 映射没有关键路径的 JSON 字符串数组时遇到问题。我的课程基于 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;
    };
})

任何建议将不胜感激,谢谢

4

2 回答 2

2

基本上,您可以在返回的代理对象上使用除description. 这意味着不将其作为变量提供给NSLog.

真实对象将存储到数据存储中。根据您需要做什么,您可能想要直接检索它们(通过获取或使用托管对象 ID)。

于 2013-11-03T23:16:30.393 回答
1

我不知道 RestKit,但似乎 4 个调用featureMapping都插入了一个新的RKAttributeMapping- 这似乎没有任何意义。也许这就是您的featureType财产出现问题的原因。

于 2013-11-03T11:24:41.937 回答