patrick-fitzgerald
patrick-fitzgerald

Reputation: 2669

RestKit Mapping a string array without key paths

I'm having a problem mapping a JSON array of strings with no key paths using RestKit 0.20.3. My classes are based on the the example in the RestKit wiki.

JSON:

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

Classes:

@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

Mapping Setup:

+ (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;
}

When debugging the feature.featureType object in Xcode, a RKMappingSourceObject object is returned with the value stored in an object property, which I can't access. When printing feature.featureType.class, NSCFString is printed.

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

Log output:

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;
    };
})

Any suggestions would be appreciated, Thanks

Upvotes: 3

Views: 1494

Answers (2)

Wain
Wain

Reputation: 119021

Basically, you can use any method on the returned proxy object other than description. That means not supplying it as a variable to NSLog.

The true objects will be stored into the data store. Depending on what you need to do you may want to go and retrieve them directly (by fetching or using the managed object id).

Upvotes: 2

Mundi
Mundi

Reputation: 80265

I don't know about RestKit, but it seems that the 4 calls to featureMapping are each inserting a new RKAttributeMapping - that does not seem to make any sense. Maybe this is the reason you have a problem with your featureType property.

Upvotes: 1

Related Questions