3
votes

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

2
Not clear what your problem is. Everything looks correct. Where / when are you debugging? What is failing to work for you? - Wain
Hi, sorry for not being clearer, the problem is that I'm unable to get the feature.featureType value as an NSString, it is returning as a NSCFString instead. - patrick-fitzgerald
NSCFString is a type of NSString (it's a class cluster)... - Wain
How can I extract the string value from the NSCFString? The NSCFString description is printed as: test ({HTTP ={request ={URL = "localhost:3000/api/v1/test";headers ={};method = GET;}; - patrick-fitzgerald
When are you printing it out? Is should only give that log during the mapping process because a proxy object is used. Once the mapping is complete you should just have the string ('test' in this case). - Wain

2 Answers

2
votes

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).

1
votes

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.