【问题标题】:RKMappingResult has objects, but they NSLog as nullRKMappingResult 有对象,但它们 NSLog 为 null
【发布时间】:2023-03-20 10:40:01
【问题描述】:

我在做什么

使用RestKit,我发出GET 请求以获取JSON 对象,该对象包含填充UITableView 的用户对象数组。我将该数组传递给一个名为users 的私有NSArray,它变为_users(我对此仍然很模糊)。这有效,并且表格填充得很好。我可以通过其他方法访问_users 数组中的各个对象,例如[UITableViewCell cellForRowAtIndex]

然而,与此同时,我将数据拉下来,在我从[RKObjectManager getObjectsAtPath...]success 块内部调用[self.tableView reloadData] 之前,我想稍微处理一下各个对象。

我的问题

使用[RKObjectManager getObjectsAtPath parameters success failure]success 按预期返回RKMappingResult,我将其数组传递给_users,它填充了我的UITableView。这可行,但在同一个success 块中,我尝试NSLog'ing _users[i] 并返回*nil description*。我知道这些值是在某个时候设置的,因为我通过在另一种方法中调用 _users[i] 来填充我的 UITableViewCells

希望能提供更多有用的信息

当我从 success 块内部 NSLog(@"%@", _users) 并知道数组中有 3 个对象时,我看到了:

( (null), (null), (null) ).

我可以提供更多信息,但我不知道该放什么。我也可以展示我的代码,但它基本上不在 RestKit 文档中。

用户对象

@interface User : NSObject

@property (nonatomic, strong) NSString *id;
@property (nonatomic, strong) NSString *email;
@property (nonatomic, strong) NSString *username;
@property (nonatomic, strong) NSString *fullName;
@property (nonatomic, strong) NSString *bio;
@property (nonatomic) NSDate *dob;
@property (nonatomic, strong) NSString *avatar;
@property (nonatomic, strong) NSString *avatarMeta;
@property (nonatomic, strong) NSString *location;
@property (nonatomic, strong) NSURL *url;
@property (nonatomic, strong) NSString *enabled;

@end

RKObjectManager 示例

*注意 - 出于安全原因,某些部分已被删除

- (void)loadUsers {
    NSMutableDictionary *params = [[NSMutableDictionary alloc] initWithDictionary:@{@"token": self.token}];
    NSString *path = @"/api/v1/g/feed";
    if ( self.cursor ) {
        path = [NSString stringWithFormat:@"%@/%@", path, self.cursor];
    }
    [[RKObjectManager sharedManager] getObjectsAtPath:path
                                       parameters:params
                                          success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
                                              _users = mappingResult.array;
                                              NSLog(@"Last User: %@",_users[0]); // *nil description*
                                              NSLog(@"Array: %@", _users); // ((null),(null),(null),(null),(null),(null),(null),(null),(null),(null))
                                              [self.tableView reloadData]; // table gets populated correctly
                                          }
                                          failure:^(RKObjectRequestOperation *operation, NSError *error) {
                                              NSLog(@"No feed available: %@", error);
                                          }];
}

【问题讨论】:

    标签: ios objective-c rest restkit objective-c-blocks


    【解决方案1】:

    很可能您正在覆盖description 方法,可能是通过在用户类中添加您自己的具有该名称的属性并将其设置为空字符串。这打破了一切。您应该将该属性重命名为 overview 或类似名称并更新您的使用情况。

    【讨论】:

    • 嗨@Wain,感谢您的帮助。我应该提到,我根据与我类似的其他 SO 帖子专门将对象键从 description 更改为 info,但它没有任何帮助。
    • 嗨@wain,我已经用一些代码和信息更新了我的原始帖子。如果您还想看其他内容,请告诉我。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2013-01-12
    • 1970-01-01
    • 2014-03-29
    • 2021-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多