2014-09-29 59 views
0

我試圖使用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

回答

0

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有一些編程指南和其他參考資料,以幫助您入門。