尝试映射对象时,RestKit崩溃

时间:2014-06-24 07:58:58

标签: ios objective-c restkit restkit-0.20

这是崩溃日志:

2014-06-24 10:46:34.725 [30190:f03] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<CropTypes 0x162638> valueForUndefinedKey:]: this class is not key value coding-compliant for the key crops.'

我有3个班级:

@interface CropTypes : NSObject

@property (strong, nonatomic) ErrorStatus *errorStatus;
@property (strong, nonatomic) NSArray *crops;

@end

@interface ErrorStatus : NSObject

@property (strong, nonatomic) NSNumber *errorCode;
@property (strong, nonatomic) NSString *errorMessage;
@property (strong, nonatomic) NSNumber *success;

@end

@interface Crop : NSObject

@property (strong, nonatomic) NSNumber *idNum;
@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSString *url;

@end

这是JSON响应:

{
  ErrorStatus: {
    ErrorCode: 0,
    ErrorMessage: null,
    Success: true
  },
  CropTypes: [
    {
      Id: 1,
      Name: "NUM",
      Url: ""
    },
    {
      Id: 2,
      Name: "NUM",
      Url: ""
    },
    {
      Id: 3,
      Name: "NUM",
      Url: ""
    },
    {
      Id: 4,
      Name: "NUM",
      Url: ""
    },
    {
      Id: 5,
      Name: "NUM",
      Url: ""
    },
    {
      Id: 6,
      Name: "NUM",
      Url: ""
    }
  ]
}

这是我映射对象的方式:

- (void)initRoutes
{
    RKRequestMethod method = RKRequestMethodGET;

    RKRoute *cropTypeRoutes = [RKRoute routeWithClass:[CropTypes class] pathPattern:@"GetCropType" method:method];
    cropTypeRoutes.shouldEscapePath = YES;
    [self.manager.router.routeSet addRoute:cropTypeRoutes];
}

- (void)initMappingObjectsAndDiscriptors
{
    RKRequestMethod method = RKRequestMethodGET;

    RKObjectMapping *errorMapping = [RKObjectMapping mappingForClass:[ErrorStatus class]];
    [errorMapping addAttributeMappingsFromDictionary:@{ @"ErrorCode" : @"errorCode", @"ErrorMessage" : @"errorMessage", @"Success" : @"success" }];

    //          Corp type service
    //
    RKObjectMapping *cropItemMapping = [RKObjectMapping mappingForClass:[Crop class]];
    [cropItemMapping addAttributeMappingsFromDictionary:@{ @"Id" : @"idNum", @"Name" : @"name", @"Url" : @"url" }];

    RKObjectMapping *cropTypesMapping = [RKObjectMapping mappingForClass:[CropTypes class]];
    [cropTypesMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"CropTypes" toKeyPath:@"crops" withMapping:cropItemMapping]];
    [cropTypesMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"ErrorStatus" toKeyPath:@"errorStatus" withMapping:errorMapping]];

    [[RKObjectManager sharedManager] addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:cropTypesMapping
                                                                                                        method:method
                                                                                                   pathPattern:@"GetCropType"
                                                                                                       keyPath:nil
                                                                                                   statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]];
}

似乎有什么问题?这可能是我使用RestKit的100次,并且之前没有任何改变。

提前致谢。

0 个答案:

没有答案