RestKit object mapping fails depending on property name - objective-c

I'm seeing some strange behavior with RestKit and mapping an object. In my destination object, if I have an NSString * property named 'description', then the entire mapping fails. Changing this to any other name, or commenting it out, everything succeeds. This happens regardless if I have a map set up for the property, and I can reproduce it in simple test classes.
The JSON the server is returning:
{
id = 1;
name = item1;
},
{
id = 2;
name = item2;
}
The object I want to map to, SimpleObject.h. Note that neither the description, nor the nonexistant property is in the JSON, meaning I don't think it's because 'description' is missing:
#import <Foundation/Foundation.h>
#interface SimpleObject : NSObject
#property (nonatomic, assign) NSInteger identifier;
#property (nonatomic, retain) NSString *name;
#property (nonatomic, retain) NSString *description;
#property (nonatomic, retain) NSString *nonexistant;
#end
SimpleObject.m:
#import "SimpleObject.h"
#implementation SimpleObject
#synthesize identifier;
#synthesize name;
#synthesize description;
#synthesize nonexistant;
#end
I create the mapping in my AppDelegate:
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURLString:#"http://127.0.0.1/test"];
// Setup our object mappings
RKObjectMapping *testMapping = [RKObjectMapping mappingForClass:[SimpleObject class]];
[testMapping mapKeyPath:#"id" toAttribute:#"identifier"];
[testMapping mapKeyPath:#"name" toAttribute:#"name"];
// Register our mappings with the provider using a resource path pattern
[objectManager.mappingProvider setObjectMapping:testMapping forResourcePathPattern:#"/products"];
Inside my didLoadObjects method, I have:
- (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects
{
NSLog(#"Loaded products: %#", objects);
SimpleObject *obj = [objects objectAtIndex:0];
NSLog(#"Loaded product ID %d -> Name: %# ", obj.identifier, obj.name);
}
This outputs:
Finished performing object mapping. Results: {
"" = (
(null),
(null)
);
}
If I comment out the description property (but leave in nonexistant), then everything works:
Finished performing object mapping. Results: {
"" = (
"<SimpleObject: 0x8a4d040>",
"<SimpleObject: 0x8a50130>"
);
}
Is there a reason the mapping is failing only for this property name? If I change the name to anything other than 'description', it also succeeds.

It is conflicting with description property of NSObject. Rename it to other(like desc).

Related

How to Incorporate an Array Into Model Object With Mantle?

I have a JSON object like so:
{
"name": "Brendan",
"images": ["some.url.to.image1",
"some.url.to.image2",
"some.url.to.image3"]
}
My class is as follows:
#interface MyModel : MTLModel <MTLJSONSerializing>
#property (nonatomic, copy) NSString *name;
#property (nonatomic, copy) NSArray *images;
#end
#implementation MYModel
+ (NSDictionary*)JSONKeyPathsByPropertyKey {
return #{
#"name" : #"name",
#"images" : #"images"
};
}
#end
I can verify that MYModel object has name properly set, but images is set to null. How can I populate an array of strings with Mantle?
Update: Apparently mtl_externalRepresentationArrayTransformerWithModelClass: is deprecated. This might work:
[MTLJSONAdapter arrayTransformerWithModelClass:[NSString class]];
You need to specify value transformer for key images as Array value transformer. You can do this with class method (on your MyModel class) with correct name. Something like this might work. I have not tested the code.
+ (NSValueTransformer *)imagesTransformer
{
return [NSValueTransformer mtl_externalRepresentationArrayTransformerWithModelClass:[NSString class]];
}

RestKit Identical To-One Mappings on Separate Objects Excepts

I know what I'm doing is quite simple, and I'm a bit confused as to what I'm doing wrong here.
I'm trying to have a given key on a JSON object always map to the same Objective-C class. This key has the same name on all of the JSON object's that have it (device).
I have two classes, Foo and Bar that each have a device property:
#interface Foo
#property (strong, nonatomic) Device * device;
#end
#interface Bar
#property (strong, nonatomic) Device * device;
#end
#interface Device
#property (strong, nonatomic) NSString * deviceId;
#property (strong, nonatomic) NSString * deviceName;
#end
And for the JSON that's sent by my server for Foo and Bar, the device is represented like so:
{
"device": {
"deviceId": "someId",
"deviceName": "someName"
}
}
In a configuration function for RestKit in my application, I have the following:
RKObjectMapping * responseMapping = [RKObjectMapping mappingForClass: model[#"class"]];
if ( model[#"class"] == [Foo class] ||
model[#"class"] == [Bar class] ){
NSArray * defaultRequestKeys = #[#"id", #"createdAt", #"updatedAt"];
NSArray * defaultModelKeys = #[#"objectId", #"createdAt", #"updatedAt"];
NSArray * deviceRequestKeys = [#[#"deviceName", #"deviceId"] arrayByAddingObjectsFromArray: defaultRequestKeys];
NSArray * deviceModelKeys = [#[#"deviceName", #"deviceId"] arrayByAddingObjectsFromArray: defaultModelKeys];
RKObjectMapping * deviceResponseMapping;
deviceResponseMapping = [RKObjectMapping mappingForClass: [Device class]];
[deviceResponseMapping addAttributeMappingsFromDictionary: [NSDictionary dictionaryWithObjects: deviceModelKeys
forKeys: deviceRequestKeys]];
[responseMapping addRelationshipMappingWithSourceKeyPath:#"device" mapping: deviceResponseMapping];
}
However, RestKit excepts with failed: caught "NSInternalInconsistencyException", "Unable to add mapping for keyPath device, one already exists..."
Which means I'm doing something wrong.
I've tried looking all through the docs and I'm confused how I have the same key on two different objects use the same object mapping. This is for the Device to-one relationship on Foo and Bar.
In my case, I had declared this key already outside of the call to
[responseMapping addRelationshipMappingWithSourceKeyPath:#"device" mapping: deviceResponseMapping];
In another part of the code. Removing this key fixed the problem. I was adding it with:
[responseMapping addAttributeMappingsFromDictionary: responseAttributeMappings];

RestKit Object Mapping did not mapping my object

{"avatar_30":"url",
"id": 81,
"name": "\u674e\u5f3a"
}
I have my Profile Class.
#interface Profile: NSObject{
NSNumber* _identifier;
NSString* _name;
NSString* _avatar_30;
}
#property (nonatomic, retain) NSNumber* identifier;
#property (nonatomic, retain) NSString* name;
#property (nonatomic, retain) NSString* avatar_30;
#end
#implementation Profile
#synthesize identifier=_identifier;
#synthesize name=_name;
#synthesize avatar_30 = _avatar_30;
And then I build my manager and get mapping for Profile Class like these:
in AppDelegate.h
#interface AppDelegate : UIResponder
in AppDelegate.m
RKObjectMapping* profileMapping = [RKObjectMapping mappingForClass:[Profile class]];
[profileMapping mapKeyPath:#"id" toAttribute:#"identifier"];
[profileMapping mapKeyPath:#"name" toAttribute:#"name"];
[profileMapping mapKeyPath:#"avatar_30" toAttribute:#"avatar_30"];
RKObjectManager* manager = [RKObjectManager objectManagerWithBaseURL:_baseURL];
//[manager.mappingProvider setMapping:profileMapping forKeyPath:#"%#"];
[manager loadObjectsAtResourcePath:#"/api/v2/userinfo/100/" objectMapping:profileMapping delegate:self];
I can get the jason correctly, however the profile can not be mapped.
Got a JSON response back from GET!
2012-04-10 17:47:33.480 Base_01[5714:14603] D restkit.network:RKObjectLoader.m:198 Found directly configured object mapping, creating temporary mapping provider for keyPath '%#'
2012-04-10 17:47:33.482 Base_01[5714:fb03] Load collection of Profiles: (
)
Could any have any idea for this problem ? Thanks for your help!
In the method
- (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects{
are there any objects in the "objects" array?
This should contain all your Profile objects.

Mapping nested objects with RestKit in Xcode using Objective-C

With the help of mja, I managed to successfully set up simple object mapping using RestKit and Objective-C. Please see my previous question here.
My next step was to attempt to deal with nested JSON in the same way.
My JSON looks like this, with the outer being a CandidatePhrase with some nested 'Votes':
{ "Id":33696,
"Phrase": "phrase",
"BadCount":0,
"Votes":[{"Id":447,"OriginalId":33696,"Votes":2,"Translation":"translation 1"},
{"Id":746,"OriginalId":33696,"Votes":1,"Translation":"translation 2"},
{"Id":747,"OriginalId":33696,"Votes":1,"Translation":"translation 3"}
]}
I created a relationship in my AppDelegate as follows:
[candidatePhraseMapping mapKeyPath:#"votes" toRelationship:#"vote" withMapping:voteMapping];
When I call make my request in my controller, I'm able to deal with the rest of the CandidatePhrase okay, but am not really sure how to map the nested 'Vote' objects into an array so I can use them in a TableView
(pseudo-code something like this...)
// Store the votes in an array
_votes = [[NSArray alloc] initWithObjects:myCandidatePhrase.votes, nil];
Here's my CandidatePhrase Object
#interface CandidatePhrase : NSObject
#property (nonatomic, retain) NSNumber* ident;
#property (nonatomic, retain) NSNumber* badcount;
#property (nonatomic, retain) NSString* phrase;
#property (nonatomic, retain) NSArray* votes;
#end
and my Vote object
#interface Vote : NSObject
#property (nonatomic, retain) NSNumber* ident;
#property (nonatomic, retain) NSNumber* originalId;
#property (nonatomic, retain) NSNumber* votecount;
#property (nonatomic, retain) NSString* translation;
+ (id)voteWithTranslationId:(NSNumber *)ident translation:(NSString *)translation;
#end
Any help would be much appreciated.
EDIT
Below is my mapping code
// Votes Mapping
RKObjectMapping* voteMapping = [RKObjectMapping mappingForClass:[Vote class]];
[voteMapping mapKeyPath:#"Id" toAttribute:#"ident"];
[voteMapping mapKeyPath:#"OriginalId" toAttribute:#"originalId"];
[voteMapping mapKeyPath:#"Votes" toAttribute:#"votecount"];
[voteMapping mapKeyPath:#"Translation" toAttribute:#"translation"];
[[manager mappingProvider] addObjectMapping:voteMapping];
// Candidate Phrase Mapping
RKObjectMapping *candidatePhraseMapping = [RKObjectMapping mappingForClass:[CandidatePhrase class]];
[candidatePhraseMapping mapKeyPath:#"Id" toAttribute:#"ident"];
[candidatePhraseMapping mapKeyPath:#"Phrase" toAttribute:#"phrase"];
[candidatePhraseMapping mapKeyPath:#"BadCount" toAttribute:#"badcount"];
[candidatePhraseMapping mapKeyPath:#"Votes" toRelationship:#"votes" withMapping:voteMapping];
[[manager mappingProvider] addObjectMapping:candidatePhraseMapping];
For clarity also, here's how I'm attempting to access the vote items on the controller
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObject:(id)object
{
CandidatePhrase *myCandidatePhrase = (CandidatePhrase*)object;
self.candidateText.text = myCandidatePhrase.phrase; <-- works fine
_votes = [[NSArray alloc] initWithObjects:myCandidatePhrase.votes, nil];
for (id o2 in _votes) {
//Vote *vote = o2;
NSLog(#"Item name: %#", o2); <-- sees object but crashes
}
NSLog(#"Votes: %#", myCandidatePhrase.votes);
_votes = [[NSArray alloc] initWithObjects:myCandidatePhrase.votes, nil];
[_votesTableView reloadData];
}
and my table is binding with
Vote *vote = [_votes objectAtIndex:indexPath.row];
cell.textLabel.text = vote.translation;
You do not need to manually manage the nested NSArray. I believe the problem might be just a simple typo, as you map keyPath "votes", but your json contain "Votes" with capital "V".
[candidatePhraseMapping mapKeyPath:#"Votes" toRelationship:#"votes" withMapping:voteMapping];
If this doesn't help feel free to leave a comment and update your question with voteMapping.
Also, the contents of the didLoadObject can be simplified:
//in .h file
#property (nonatomic, retain) NSArray* votes;
// in implementation
#synthesize votes;
...
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObject:(id)object
{
CandidatePhrase *myCandidatePhrase = (CandidatePhrase*)object;
self.candidateText.text = myCandidatePhrase.phrase;
self.votes = myCandidatePhrase.votes;
}

How to access the object

I would like to access the the _news items in a loop. But don't know how to get this done.
My Game.m looks like
#import "Game.h"
#implementation Game
#synthesize homename = _homename;
#synthesize guestname = _guestname;
#synthesize date = _date;
#synthesize gametype = _gametype;
#synthesize news = _news;
#synthesize gameId = _gameId;
-(NSString*)description{
return [NSString stringWithFormat:#"%# gegen %# (%#)", self.homename, self.guestname, self.gametype];
}
#end
My Game.h looks like
#import <Foundation/Foundation.h>
#import <RestKit/RestKit.h>
#import "News.h"
#interface Game : NSObject {
NSString* _homename;
NSString* _guestname;
NSString* _date;
NSString* _gametype;
NSNumber* _gameId;
// News* news;
#public
NSArray* _news;
}
#property (nonatomic, retain) NSString* homename;
#property (nonatomic, retain) NSString* guestname;
#property (nonatomic, retain) NSString* date;
#property (nonatomic, retain) NSString* gametype;
//#property (nonatomic, retain) News* news;
#property (nonatomic, retain) NSArray* news;
#property (nonatomic, retain) NSNumber* gameId;
#end
My News.h looks like
#import <Foundation/Foundation.h>
#import <RestKit/RestKit.h>
#interface News : NSObject {
NSString* _minute;
NSString* _title;
NSString* _bodytext;
NSString* _player;
}
#property (nonatomic, retain) NSString* minute;
#property (nonatomic, retain) NSString* title;
#property (nonatomic, retain) NSString* bodytext;
#property (nonatomic, retain) NSString* player;
#end
My news.m looks like
#import "News.h"
#implementation News
#synthesize player = _player;
#synthesize title = _title;
#synthesize bodytext = _bodytext;
#synthesize minute = _minute;
#end
And the code where I want to access the variable looks like:
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects {
NSLog(#"Loaded statuses: %#", objects);
HeaderText.text = [NSString stringWithFormat:#"%#", objects ];
// for(News* n in _news) NSLog([n _bodytext]);
// for (id object in objects) {
// NSLog(#"News = %#", object );
// }
}
The NSLog with objects looks good for the game. The next thing is that I want to do something like (above is more pseudo code than real code because I don't know how to do it right):
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects {
NSLog(#"Loaded statuses: %#", objects);
HeaderText.text = [NSString stringWithFormat:#"%#", objects ];
// loop all
for (id object in objects) {
news = objects.news;
for (id mynews in news) {
NSLog(#"News minute = %#", news.minute );
NSLog(#"News title = %#", news.title );
NSLog(#"News bodytext = %#", news.bodytext );
NSLog(#"News player = %#", news.player );
}
}
}
How to do the getter/setter methods right (so that I can use it here)?
Sorry for the surely stupid question but I don't get the point with it.
Two things:
1stly, is the _news object a private variable, without having a property declaration (getters and setters for e.g.)? The '_variableName' format is usually used to denote private variables.
2ndly, if it is not a private variable, do all the items within the _news array belong to the same class?
If so, you can do a
for (NewsObject *theNewsObject in _news)
{
//code here
}
The for(id randomObject in array) is useful when you don't know what type of object is in the array or if the objects contained in the array are of different types.
Now, again, all the objects inside the NewsObject ought to be public properties that can be accessed by other classes (they should have getters and setters).
Hope this helps. :)
EDIT FOR UPDATED QUESTION
So, if I'm getting your question correctly, you have a Game object, which has an array of News object inside it.
So, in your Game.h
NSString* _homename;
NSString* _guestname;
NSString* _date;
NSString* _gametype;
NSNumber* _gameId;
NSArray * _newsObjects; //declare it as NSMutableArray if you need to mutate it
Now, where you declare your properties;
#property(nonatomic, retain)NSArray *newsObjects
Synthesize it like you normally would in the Game.m file
You are creating the getters/setters automatically by using the #synthesize directive. It creates the getters and setters for you.
Again, from your code, it looks like the NSArray of objects that are passed through the method
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects
consist of Game objects.
So, to access the News object from within the array of Game objects, import News.h and Game.h in the class where this method is being executed, and do the following:
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects {
NSLog(#"Loaded statuses: %#", objects);
// loop all
for (Game *gameObject in objects) {
NSArray *newsObjectArray = [gameObject newsObjects] //or gameObject.newsObject
for (News *mynews in newsObjectArray) {
NSLog(#"News minute = %#", mynews.minute );
NSLog(#"News title = %#", mynews.title );
NSLog(#"News bodytext = %#", mynews.bodytext );
NSLog(#"News player = %#", mynews.player );
}
}
}
What I found in your code that there was code that was being executed which was not declared in any part of the example that you posted.
What the code above will do is it will look through the Game object array (called objects).
Then, for each gameObject within that array, it will loop through the array of newsObject for that gameObject.
Hope this helps. :)