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

我有一个问题,使用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;
    };
})

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


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

真正的对象将被存储到数据存储中。 取决于你需要做什么,你可能想要去直接检索它们(通过获取或使用托管对象ID)。


我不知道RestKit,但似乎对featureMapping的4次调用每次都插入一个新的RKAttributeMapping - 这似乎没有任何意义。 也许这是你的featureType属性有问题的原因。

链接地址: http://www.djcxy.com/p/49197.html

上一篇: RestKit Mapping a string array without key paths

下一篇: Using RestKit to map an NSArray into JSON with dynamic nested attributes?