Search code examples
iosobjective-cunrecognized-selector

Retrieving data from database field, terminating with uncaught exception of type NSException?


I'm retrieving data (thirdLink) from my database with the following code:

    NSMutableDictionary *viewParamsDogs = [NSMutableDictionary new];
    [viewParamsDogs setValue:@"mydogs" forKey:@"view_name"];
    [DIOSView viewGet:viewParamsDogs success:^(AFHTTPRequestOperation *operation, id responseObject) {


        //DOG PHOTO


      self.dogData = [responseObject mutableCopy];

        NSDictionary *dic = [responseObject valueForKey: @"field_pet_photo_path"];
                             NSArray *arr = [dic valueForKey: @"und"];
                             NSDictionary *dic2= [arr objectAtIndex : 0];
                             NSString *path = [dic2 valueForKey: @"safe_value"];


    if([path length]>0) {

            NSString *ImageURL = path;
            NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:ImageURL]];

            self.dogimageView.image = [[UIImage alloc] initWithData:imageData];

        } else {

            NSString *ImageURL = @"paw.png";
            NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:ImageURL]];
            self.dogimageView.image = [UIImage imageWithData:imageData];
      }

  } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Failure: %@", [error localizedDescription]);
    }];

The field is populated and the data is returned, but this line is throwing me the following error:

  NSString *thirdLink = responseObject[@"field_pet_photo_path"][@"und"][0][@"safe_value"];

[__NSCFArray objectForKeyedSubscript:]: unrecognized selector sent to instance 0x149857d40 terminating with uncaught exception of type NSException

Does anyone know how I should be writing this line to properly grab the returned data (url) in field_pet_photo_path? See returned data below:

"field_pet_photo_path" =         {
            und =             (
                                {
                    format = "<null>";
                    "safe_value" = "/files/stored/1460652721.jpg";
                    value = "/files/stored/1460652721.jpg";
                }
            );
        };    

Solution

  • NSDictionary *dic = [responseObject valueForkey : @"field_pet_photo_path];
    NSArray *arr = [dic valueForkey : @"und"];
    NSDictionary *dic2= [arr objectAtIndex : 0];
    NSString *path = [dic2 valueForkey : @"safe_value"];
    

    If required key name or key string with double quote then use like, @"\"safe_value\""

    Hope this will help :)

    Update :

    Try to replace NSString *path = [dic2 valueForkey : @"safe_value"]; with

    NSString* path = [NSString stringWithFormat:@"%@", [dic2 valueForkey : @"safe_value"]];