在自定义表视图控制器中的响应中加载数据时的异常错误IOS

问题描述:

做了一个自定义表视图控制器,其中调用viewDidLoad方法中的服务。该服务被调用一些参数,它是GET请求。响应即将细但它示出了一个异常错误此,终止应用程序由于未捕获的异常在自定义表视图控制器中的响应中加载数据时的异常错误IOS

“NSUnknownKeyException”,原因:“[< __NSCFString 0x60000036a140> valueForUndefinedKey:]:这个类不是关键值coding-符合关键数据。“

从服务我的回答是这

{"data":[{"id":"139559","first_name":"Shoaib Anwar","last_name":null,"address":null,"mobile":"03233008757","city":null,"date":"2017-08-10","date_of_birth":"1992-08-10"}]}.

应用程序崩溃的时候,我很困惑,为什么它显示此错误。

我的代码是这样的,

NSString *savedValue = [[NSUserDefaults standardUserDefaults] 
          stringForKey:@"number"]; 
    NSString *[email protected]"My Url"; 
    NSString *string3 = [url stringByAppendingString:savedValue]; 

    NSString *lastArray = @"&type=json"; 
    string3 = [string3 stringByAppendingString:lastArray]; 
    NSLog(@"Mmm %@",savedValue); 
    NSLog(@"Mmm %@",string3); 

    NSString *targetUrl = [NSString stringWithFormat:@"%@",string3]; 
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
    [request setHTTPMethod:@"GET"]; 
    [request setURL:[NSURL URLWithString:targetUrl]]; 

    [[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler: 
     ^(NSData * _Nullable data, 
     NSURLResponse * _Nullable response, 
     NSError * _Nullable error) { 

      NSString *myString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
      NSLog(@"Data received: %@", myString); 
      NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data 
                   options:NSJSONReadingMutableContainers 
                   error:nil]; 
      NSString *value = json[@"data"]; 



    _Ids = [NSMutableArray array]; 

    for (NSDictionary * oneCustomer in value) 
    { 
     // Create a new Customer record 

     ViewProfile * newCustomer = [[ViewProfile alloc] init]; 



     newCustomer.ids = [oneCustomer objectForKey:@"id"]; 
     NSLog(@"ID: %@ ", newCustomer.ids); 

     newCustomer.fname = [oneCustomer objectForKey:@"first_name"]; 
     NSLog(@"Fname: %@ ", newCustomer.fname); 

     newCustomer.lname = [oneCustomer objectForKey:@"last_name"]; 

     NSLog(@"Lname: %@ ", newCustomer.lname); 

     newCustomer.address = [oneCustomer objectForKey:@"address"]; 
     NSLog(@"Address: %@ ", newCustomer.address); 

     newCustomer.mobile = [oneCustomer objectForKey:@"mobile"]; 
     NSLog(@"Mobile: %@ ", newCustomer.mobile); 

     newCustomer.city = [oneCustomer objectForKey:@"city"]; 
     NSLog(@"City: %@ ", newCustomer.city); 

     newCustomer.date = [oneCustomer objectForKey:@"date"]; 
     NSLog(@"Date: %@ ", newCustomer.date); 

     newCustomer.dob = [oneCustomer objectForKey:@"date_of_birth"]; 
     NSLog(@"DOB: %@ ", newCustomer.dob); 

     // Add our new Customer record to our NSMutableArray 
     [_Ids addObject:newCustomer]; 

    } 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     // This code will run once the JSON-loading section above has completed. 

     [self.tableView reloadData]; 
    }); 




    NSString *status=[myString valueForKey:@"data"]; 
    NSLog(@"Status:%@",status); 




}] resume]; 
+0

请分享一些代码。 –

+0

检查它。 @NaumanMalik – Oneeb

您的回应是一个JSON字符串。

尝试将myString属性转换为NSData类型,然后传递给NSJsonSerialisation类。

NSData *jsonData = [myString dataUsingEncoding:NSUTF8StringEncoding]; 

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil]; 

希望这有助于:)

+0

现在得到这个错误终止应用程序,由于未捕获异常'NSInvalidArgumentException',原因:' - [NSTaggedPointerString objectForKey:]:无法识别的选择器发送到实例0xa000000617461644'。 @Basheer – Oneeb

+0

@Oneeb NSString * value = json [@“data”]; json [@“data”]将返回数组。但为什么你要分配给NSString? – Basheer

+0

比写什么? @Basheer – Oneeb

你应该注释此行

NSString *status=[myString valueForKey:@"data"]; 

或检查你的故事板或XIB

是你有标签或按钮,如图像连接

enter image description here

+0

评论它后不工作。 @Abdelahad Darwish – Oneeb

+0

@Oneeb回答更新 –

+0

每一件事情都是正确的,没有这种出口问题。 @Abdelahad Darwish – Oneeb

请尝试下面的代码并根据您的要求进行更新。

NSString *myString = @"{\"data\":[{\"id\":\"139559\",\"first_name\":\"Shoaib Anwar\",\"last_name\":null,\"address\":null,\"mobile\":\"03233008757\",\"city\":null,\"date\":\"2017-08-10\",\"date_of_birth\":\"1992-08-10\"}]}"; 

NSData *data = [myString dataUsingEncoding:NSUTF8StringEncoding]; 
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; 

NSArray *value = json[@"data"]; 

for (NSDictionary * oneCustomer in value) 
{ 
    // Create a new Customer record 

    NSLog(@"ID: %@ ", [oneCustomer objectForKey:@"id"]); 
    NSLog(@"Fname: %@ ", [oneCustomer objectForKey:@"first_name"]); 
} 

崩溃问题的原因NSString *status=[myString valueForKey:@"data"];

+0

谢谢,我得到了我的答案。:) @Neha Gupta – Oneeb