objective-c - Restkit 对象映射不起作用

标签 objective-c ios ios6 restkit xcode4.5

我正在尝试学习 restkit,现在正在研究对象映射。这是我尝试接收和处理的 JSON(由 ASP.NET MVC Web API 生成):

[
    {
        "Id": 0,
        "Title": "Test",
        "Location": "Test",
        "Score": {
            "Hill": 10,
            "LVille": 12
        },
        "TimeStart": "2012-11-10T12:30:00",
        "TimeEnd": "2012-11-10T13:20:00",
        "Gender": 0,
        "State": 0
    },
    {
        "Id": 0,
        "Title": "ee",
        "Location": "12:00",
        "Score": {
            "Hill": 13,
            "LVille": 0
        },
        "TimeStart": "12:00 PM",
        "TimeEnd": "12:00 PM",
        "Gender": 1,
        "State": 1
    }
]

被映射到的对象是(SBGame):

@interface SBGame : NSObject

@property (nonatomic, copy) NSNumber* Id;
@property (nonatomic, copy) NSString* Title;
@property (nonatomic, copy) NSString* Location;
@property (nonatomic, strong) SBScore* Score;
@property (nonatomic, copy) NSString* TimeStart;
@property (nonatomic, copy) NSString* TimeEnd;
@property (nonatomic, copy) NSNumber* Gender;
@property (nonatomic, copy) NSNumber* State;

@end

和 SBScore:

@interface SBScore : NSObject
@property (nonatomic, copy) NSNumber* Hill;
@property (nonatomic, copy) NSNumber* LVille;
@end

最后但同样重要的是对象映射

RKObjectMapping *ScoreMapping = [RKObjectMapping mappingForClass:[SBScore class]];
[ScoreMapping addAttributeMappingsFromArray:@[@"Hill", @"LVille"]];


RKObjectMapping *GameMapping = [RKObjectMapping mappingForClass:[SBGame class]];
[GameMapping addAttributeMappingsFromDictionary:@{
    @"Id" : @"Id",
    @"Title" : @"Title",
    @"Location" : @"Location",
    @"TimeStart" : @"TimeStart",
    @"TimeEnd" : @"TimeEnd",
    @"Gender" : @"Gender",
    @"State" : @"State",
 }];

[GameMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"score" toKeyPath:@"score" withMapping:ScoreMapping]];


RKResponseDescriptor *decp = [RKResponseDescriptor responseDescriptorWithMapping:GameMapping pathPattern:@"/Games" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:decp];

毕竟我收到的错误是

2012-12-29 16:49:19.553 Scoreboard[3004:c07] Failure with error Error Domain=org.restkit.RestKit.ErrorDomain Code=1001 
"Unable to find any mappings for the given content" UserInfo=0x75c1460 {DetailedErrors=(
), NSLocalizedDescription=Unable to find any mappings for the given content, keyPath=null}

我是 RestKit 的新手,我已经浏览过 The Object Mapping Tutorial但它仍然不起作用。非常感谢任何想法。

最佳答案

我想通了。当我创建默认实例时,我创建了基本 URL www.example.com/api,然后将绑定(bind)和 getObjects 方法设置为 /Games

我应该做的是让基本 URL www.example.com 将路径模式设置为 /api/Games 并调用 上的 getObjects 方法/api/游戏

所以从显示的代码来看

RKResponseDescriptor *decp 
= [RKResponseDescriptor responseDescriptorWithMapping:GameMapping
                                          pathPattern:@"/Games" 
                                              keyPath:nil
                                          statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

更改为

RKResponseDescriptor *decp 
= [RKResponseDescriptor responseDescriptorWithMapping:GameMapping
                                          pathPattern:@"/api/Games"
                                              keyPath:nil
                                          statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

关于objective-c - Restkit 对象映射不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14086301/

相关文章:

ios - 向下滚动然后向上滚动后 UITableViewCells 发生变化

ios - iOS 中的不可移除通知以编程方式

ios - 无法将类型 'CFString' 的值转换为预期的参数类型 'UnsafePointer<Void>'(又名 'UnsafePointer<()>')

ios - Swift:JSON 写入中的无效类型(UIImage)

google-chrome - 如何在 chrome/safari mobile IOS 6 上查看 javascript 错误

ios - 更改 SKSpriteNode 图像

ios - 使用 Objective C 生成 mongodb 兼容 id

ios - swift中的DES加解密算法

ios - UILabel 中不同大小的顶部对齐文本

html - 使用 iOS 6 查看嵌入式 youtube 视频 - iframe 后的链接不可点击