【问题标题】:Unable to Save AFNetworking responseObject无法保存 AFNetworking 响应对象
【发布时间】:2015-01-06 07:07:58
【问题描述】:

我正在使用AFNetworking 调用网络服务并将返回数据保存在NSDictionary 对象中。但即使数据成功登录NSLog(),也没有存储任何内容。

这是我的字典:

@property (strong, nonatomic) NSDictionary *newsItems;

这是我的代码:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSDictionary *parameters = @{@"key": @"keyy", @"q": @"ads" };
    [manager POST:BaseURLString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.newsItems = (NSDictionary *) responseObject;
        NSLog(@"JSON: %@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];

【问题讨论】:

  • 你的代码是正确的,你能把NSLogself.newsItemsresponseObject放在一起吗?
  • 它可能会返回 NSArrayNSDictionary。在转换之前检查响应类型。
  • 请在控制台中发布responseObject的NSLog。
  • 它应该返回字典数组

标签: ios objective-c ios7 afnetworking


【解决方案1】:

首先,使用以下行检查您是否收到来自 Web 服务的任何响应:

NSLog(@"RESPONSE : %@", operation.responseString);

其次,如果你的网络服务应该返回一个字典数组,那么你应该在字典上声明一个数组。

@property (strong, nonatomic) NSArray *newsItems;

而不是

@property (strong, nonatomic) NSDictionary *newsItems;

【讨论】:

    【解决方案2】:

    您需要声明NSArray 而不是NSDictionary,如下所示:

    @property (strong, nonatomic) NSArray *newsItems;

    并分配responseObject,如下所示:

    self.newsItems = (NSArray *) responseObject;

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-03
      • 1970-01-01
      • 1970-01-01
      • 2013-12-08
      相关资源
      最近更新 更多