映射后一些关键是nil

时间:2013-12-13 17:03:08

标签: ios restkit

我有一个映射

@interface SynchObj : NSObject


    @property (nonatomic, copy)   NSString *name;
    @property (nonatomic, assign) NSString *server_id;
    @property (nonatomic, assign) NSString *app_id;
    @property (nonatomic, strong) NSString *operation;
    @property (nonatomic, strong) NSString *table;
    @property (nonatomic, strong) NSString *event_data_s;
    @property (nonatomic, strong) NSString *event_data_c;
    @property (nonatomic, strong) NSString *fields;
    @property (nonatomic, strong) NSString *msgStr;
    @property (nonatomic, strong) NSString *code;
    @property (nonatomic, strong) NSString *status;

@end

在我的地图类中:

 mappingDict = @{

                                  @"server_id"    :@"server_id",
                                  @"app_id"       :@"app_id",
                                  @"operation"    :@"operation",
                                  @"table"        :@"table",
                                  @"event_data_s" :@"event_data_s",
                                  @"event_data_c" :@"event_data_c",
                                  @"fields"       :@"fields",
                                  @"status"       :@"status"
                                  };
 responseMapping = [RKObjectMapping mappingForClass:[SynchObj class]];
    [responseMapping addAttributeMappingsFromDictionary:mappingDict];

我从Restkit

收到以下回复
response.body={"response":{"operation":"create","status":"200","event_data_c":"2013-12-13 17:39:45","event_data_s":"2013-12-13 17:58:10","table":"MYTABLE","app_id":1,"server_id":31}

但是server_id属性始终是未定义或不正确的指针。 所有其他人都还可以。 为什么呢?

1 个答案:

答案 0 :(得分:3)

我想这是因为你应该使用NSNumber *而不是NSString *server_id。同时使用NSNumber作为app_id,因为JSON返回的是整数而不是字符串。

编辑 -

报废。这是因为您对assign使用copy而不是NSString,因此您不会告诉编译器应该保留这些值,导致在您访问它之前释放该对象。无论如何,你仍然应该使用NSNumbers

@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSNumber *server_id;
@property (nonatomic, copy) NSNumber *app_id;
@property (nonatomic, copy) NSString *operation;
@property (nonatomic, copy) NSString *table;
@property (nonatomic, copy) NSString *event_data_s;
@property (nonatomic, copy) NSString *event_data_c;
@property (nonatomic, copy) NSString *fields;
@property (nonatomic, copy) NSString *msgStr;
@property (nonatomic, copy) NSString *code;
@property (nonatomic, copy) NSString *status;