问题
I am trying to map this JSON data:
{
"Id": 1,
"Question": [
{
"Id": 1,
"PicUrl": "sample string 2",
"CorrectAnswer": "sample string 3",
"Difficulty": 4,
"CategoryId": 5,
"CategoryName": "sample string 6",
"AccountId": 7
},
{
"CorrectAnswer": "sample string 3",
"Difficulty": 4,
"CategoryId": 5,
"CategoryName": "sample string 6",
"AccountId": 7
},
{
"StartTime": "2013-10-09T00:54:46.5522592+00:00"
}
but I am having some problems. After going trough the object mapping overview of Restkit over and over again,I set my divided my data to 3 different classes. CurrentGames,items and answers. I mapped the answer class as no key-path as described in here and for the items class I created a relation to answers class. And i created an another relation from items to currentgames class. I believe I did the mapping part right. But I am missing something in my code because I am not getting the data properly. I've been trying to configure this out for a week now. If someone can help, I would appreciate big time!
this is what my console looks like:
2013-10-08 20:24:38.366 FlickRest[5126:a0b] I restkit:RKLog.m:34 RestKit logging initialized...
2013-10-08 20:24:38.518 FlickRest[5126:a0b] answers mapping <RKObjectMapping:0x8e59110 objectClass=answers propertyMappings=(
"<RKAttributeMapping: 0x8e576f0 (null) => answerss>"
)>
2013-10-08 20:24:38.519 FlickRest[5126:a0b] items mapping <RKObjectMapping:0x8e57aa0 objectClass=items propertyMappings=(
"<RKAttributeMapping: 0x8e30cd0 CategoryId => CategoryId>",
"<RKAttributeMapping: 0x8e56560 AccountId => accountId>",
"<RKAttributeMapping: 0x8e56580 Id => ID>",
"<RKAttributeMapping: 0x8e552a0 CategoryName => CategoryName>",
"<RKAttributeMapping: 0x8e552e0 Difficulty => Difficulty>",
"<RKAttributeMapping: 0x8e587c0 CorrectAnswer => correctAnswer>",
"<RKAttributeMapping: 0x8e55400 PicUrl => PicURL>",
"<RKRelationshipMapping: 0x8e567c0 Answers => Answers>"
)>
2013-10-08 20:24:38.520 FlickRest[5126:a0b] currentGames mapping <RKObjectMapping:0x8e569a0 objectClass=CurrentGames propertyMappings=(
"<RKAttributeMapping: 0x8e56a00 StartTime => StartTime>",
"<RKAttributeMapping: 0x8e56a20 GameId => GameId>",
"<RKRelationshipMapping: 0x8e595b0 Items => Items>"
)>
2013-10-08 20:24:38.549 FlickRest[5126:a0b] I restkit.network:RKObjectRequestOperation.m:180 GET 'https://xxx.net'
2013-10-08 20:24:39.287 FlickRest[5126:3507] I restkit.network:RKObjectRequestOperation.m:250 GET 'xxx.net' (200 OK / 1 objects) [request=0.7201s mapping=0.0172s total=0.7687s]
2013-10-08 20:24:39.287 FlickRest[5126:a0b] I app:WelcomeViewController.m:91 Load collection of Articles: (
"<CurrentGames: 0x8b90d70>"
)
My classes:
answers.h
#import <Foundation/Foundation.h>
@interface answers : NSObject
@property (nonatomic,copy) NSString * answerss;
@end
items.h
#import <Foundation/Foundation.h>
#import "answers.h"
@interface items : NSObject
@property (nonatomic)NSNumber *ID;
@property(nonatomic,copy)NSURL *PicURL;
@property (nonatomic,copy)NSString *correctAnswer;
@property (nonatomic)NSNumber *Difficulty;
@property(nonatomic) NSNumber *CategoryId;
@property(nonatomic,copy)NSString *CategoryName;
@property(nonatomic)NSNumber *accountId;
@end
CurrentGames.h
#import <Foundation/Foundation.h>
#import "items.h"
@interface CurrentGames : NSObject
@property (nonatomic,copy)NSNumber *GameId;
@property (nonatomic) items *Items;
@property (nonatomic,copy) NSDate *StartTime;
@end
view controller that I am using:
WelcomeViewController.m
@implementation WelcomeViewController
-(void)loadGame
{
// Mapping for the answers class. Mapped as an array with no key-path
RKObjectMapping *answersMapping = [RKObjectMapping mappingForClass:[answers class]];
[answersMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"answerss"]];
// Mapping for the items class. Mapped as relation to answers class
RKObjectMapping *itemsMapping =[RKObjectMapping mappingForClass:[items class]];
[itemsMapping addAttributeMappingsFromDictionary:@{@"Id":@"ID",
@"PicUrl":@"PicURL",
@"CorrectAnswer":@"correctAnswer",
@"Difficulty":@"Difficulty",
@"CategoryId":@"CategoryId",
@"CategoryName":@"CategoryName",
@"AccountId":@"accountId"
}];
[itemsMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"Answers" toKeyPath:@"Answers" withMapping:answersMapping]];
//Mapping for the CurrentGames class. Mapped as relation to items class.
RKObjectMapping *CurrentGamesMapping = [RKObjectMapping mappingForClass:[CurrentGames class]];
[CurrentGamesMapping addAttributeMappingsFromDictionary:@{@"GameId":@"GameId",
@"StartTime":@"StartTime"}];
[CurrentGamesMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"Items" toKeyPath:@"Items" withMapping:itemsMapping]];
// Response descriptor for all classes
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:CurrentGamesMapping method:RKRequestMethodGET pathPattern:nil keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
// Requesting data with URL
NSURL *URL = [NSURL URLWithString:@"xxx.net"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
RKObjectRequestOperation *objectRequestOperation =[[RKObjectRequestOperation alloc]initWithRequest:request responseDescriptors:@[responseDescriptor]];
[objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
RKLogInfo(@"Load collection of Articles: %@", mappingResult.array);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
RKLogError(@"Operation failed with error: %@", error);
}];
[objectRequestOperation start];
NSLog(@"answers mapping %@ ",answersMapping);
NSLog(@"items mapping %@ ",itemsMapping);
NSLog(@"currentGames mapping %@ ",CurrentGamesMapping);
}
回答1:
This property:
@property (nonatomic) answers *Answers;
should be:
@property (strong, nonatomic) NSArray *Answers;
because you have a list of answers objects.
This property:
@property (nonatomic) items *Items;
has the same issue. These 2 errors will prevent your mappings from working as you expect.
来源:https://stackoverflow.com/questions/19262700/mapping-with-restkit-with-relationships-and-no-key-path-for-some-part-of-the-dat