AFNetworking 2.0和简单的JSON

问题描述:

我试图使用AFNetworking 2.0消耗简单JSON,从JSON结果是:AFNetworking 2.0和简单的JSON

{ 
    "solicitudId": "61898", 
    "estado": "Atendida", 
    "tipoPago": null, 
    "monto": 23, 
    "mayorDerecho": 0, 
    "sistema": "SPRL" 
} 

予定义的类(solicitud.h solicitud.m)所示:

@interface SolicitudNSDictionary : NSDictionary 

- (NSString *)solicitudId; 
- (NSString *)estado; 
- (NSString *)tipoPago; 
- (NSNumber *)monto; 
- (NSNumber *)mayorDerecho; 
- (NSString *)sistema; 

@end 

的JSON是叫这里没有错误

- (IBAction)jsonButton:(id)sender { 
    // 1 
    NSString *string = [NSString stringWithFormat:@"%@solicitud?id=61898&from=MOVIL&ip=172.9.1.14", BaseURLString]; 
    NSURL *url = [NSURL URLWithString:string]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 

    // 2 
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 
    operation.responseSerializer = [AFJSONResponseSerializer serializer]; 

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 

     // 3 
     self.solicitud = (NSDictionary *)responseObject; 
     self.title = @"JSON Retrieved"; 
     [self.tableView reloadData]; 

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 

     // 4 
     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather" 
                  message:[error localizedDescription] 
                  delegate:nil 
                cancelButtonTitle:@"Ok" 
                otherButtonTitles:nil]; 
     [alertView show]; 
    }]; 

    // 5 
    [operation start]; 
} 
@end 

我的问题是我不知道如何实现的tableview从NSDictionary,在cellForRowAtIndexPath我试过,但我没有运气。

我在@interface

@property(strong) NSDictionary *solicitud; 

声明,该变量在这里设置

// 3 
     self.solicitud = (NSDictionary *)responseObject; 

我在哪里得到的错误是

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"cellName"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    NSDictionary *solicitudTmp = nil; 
    solicitudTmp = [self.solicitud]; 

    // You will add code here later to customize the cell, but it's good for now. 
    cell.textLabel.text = [self.solicitudTmp solicitudId]; 

    return cell; 
} 

在这条线

solicitudTmp = [self.solicitud]; 
+1

你的问题没有任何意义。什么不工作? “我不知道如何从NSDictionary实现tableview”是什么意思?你所显示的代码与你的问题有什么关系? – 2014-09-30 20:04:56

solicitudTmp = [self.solicitud];是语法错误。

Objective-C消息的格式为[receiver message]。上面的代码缺少一条消息。

什么你可能打算是:

cell.textLabel.text = [self.solicitud valueForKey:@"solicitudId"]; 

这就是说,有很多其他的实在令人质疑的东西,在这个问题怎么回事:

  • SolicitudNSDictionary,作为一种模式,应该是一个子类的NSObject有一个初始化程序需要一本字典(NSDictionary不应被分类)
  • 你应该让AFNetworking负责将URL参数转换为aq uery字符串。
  • 您的操作代码可以通过使用AFHTTPRequestOperationManager来改进,而不是自己构建请求。

我强烈建议您在继续之前查看一些其他资源。 Apple's Developer Site有一些编程指南和其他参考资料,以帮助您入门。