2015-04-05 58 views
-3

現在我知道這意味着它試圖獲取NSString中的所有密鑰,這是不可能的。但是,我不知道爲什麼我的字典是NSString。iOS: - [__ NSCFString allKeys]:無法識別的選擇器發送到實例0x7976c750

return [requestManager POST:@"/api/showData" 
          parameters:params 
           success:^(AFHTTPRequestOperation *operation, id responseObject) { 

            NSLog(@"the response object%@",responseObject); 

            NSMutableArray *responseObjects = [[NSMutableArray alloc] init]; 
            for (NSDictionary *node in responseObject) { 
             Model *data = [[Model alloc] initWithDictionary:node]; 
             [responseObjects addObject:data]; 
            } 
            handler(YES,responseObjects); 

           } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
            handler(NO,nil); 
           }]; 

該代碼發送到服務器的請求,並得到以下成功響應

響應對象{ 「ERROR_CODE」= 1; message =「找不到」; status = error; }

在我initWithDictionary方法我的模型類,這是我有

//Model Class 
- (id)initWithDictionary:(NSDictionary *)dict{ 
     self = [super init]; 
     if (self) { 
     NSLog(@"the dict is %@",dict); 
     if ([[dict allKeys] containsObject:@"error_code"]) { 

      } 
     return self; 
     } 

上面一行是什麼原因造成我的問題。我登錄字典在此方法中,這是我所得到的

「的字典是狀態」

+0

你沒有顯示'dict'的創建位置和設置值。編輯問題並將其添加到它。 – zaph 2015-04-05 17:00:37

+0

我編輯它,但它是通過模型類initwithdictionary創建的。 – 2015-04-05 17:05:35

+0

應用程序崩潰是因爲您沒有傳遞字典而是字符串,請檢查您從後端獲取的數據的JSON模式。 – HepaKKes 2015-04-05 17:10:43

回答

3

您需要檢查是否獲得一本字典或陣列(包含多個詞典)作爲你的responseObject,

return [requestManager POST:@"/api/showData" 
       parameters:params 
        success:^(AFHTTPRequestOperation *operation, id responseObject) { 

         NSLog(@"the response object%@",responseObject); 
         NSMutableArray *responseObjects = [[NSMutableArray alloc] init]; 

         if ([responseObject isKindOfClass:[NSDictionary class]]) { 
          Model *data = [[Model alloc] initWithDictionary:responseObject]; 
          [responseObjects addObject:data]; 
         } else if ([responseObject isKindOfClass:[NSArray class]]) { 
          for (NSDictionary *node in responseObject) { 
           Model *data = [[Model alloc] initWithDictionary:node]; 
           [responseObjects addObject:data]; 
          } 
         } 
         handler(YES,responseObjects); 

        } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
         handler(NO,nil); 
        }]; 
相關問題