Abhimanyu
Abhimanyu

Reputation: 61

-[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x9d0d720

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
     [jsonArray removeAllObjects];
     NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
     responseData = nil;
     NSMutableArray *sdf = [(NSDictionary*)[responseString JSONValue] objectForKey:@"DataTable"];
     NSMutableArray * myArray = [[NSMutableArray alloc] init];
     NSMutableDictionary * myDict = [[NSMutableDictionary alloc] init];
     if (([(NSString*)sdf isEqual: [NSNull null]])) {

        // Showing AlertView Here
     }else {
        for (int i=0; i<[sdf count]; i++) {
           myDict=[sdf objectAtIndex:i];
           [myArray addObject:[myDict objectForKey:@"RxnCustomerProfile"]];
     }

     jsonArray=[myArray mutableCopy];
     NSMutableDictionary *dict=[jsonArray objectAtIndex:0];
     if ([dict count]>1) {
        // Showing AlertView Here
      }
   }
}

Hi Everyone, I have an issue regarding the -[__NSArrayM objectForKey:]: . Tried to solve but did not get the better solution for it. Please help me to find the solution. Thanks In Advance

Below is the issues

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x19731d40'

Upvotes: 1

Views: 4023

Answers (5)

Dishant Rajput
Dishant Rajput

Reputation: 1357

YOU FETCH THE VALUE IN ARRAY FORMAT AND YOU INTEGRATE METHOD IN DICTIONARY.

Upvotes: 1

First of all it seems like your json is not actually correctly formatted. Without knowing what responseData looks like it's difficult to say exactly what is wrong. But in your code there are a few areas where it can be improved.

First of all you don't need to use [responseString JSONValue]. You can short circuit it entirely with

NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
NSArray *sdf = responseDictionary[@"DataTable"];

Now, the rest all depends on the data in responseData.

But you can make your code a little bit cleaner with (if I understand what you're trying to achieve correctly:

NSMutableArray *myArray = [NSMutableArray array];
if ([sdf isEqual:[NSNull null]]) {
    // Showing AlertView here
} else {
    for (NSDictionary *myDict in sdf) {
        [myArray addObject:dict[@"RxnCustomerProfile"]];
    }
}

// No idea what you're trying to achieve here, but here goes:
jsonArray = [myArray mutableCopy];
NSDictionary *dict = jsonArray.first;
if (dict.count > 1) {
    // Showing AlertView here
}

Some things to note. You make very liberal use of NSMutableArray and NSMutableDictionary for no apparent reason. Only use mutable if you're actually changing the array or dictionary.

Upvotes: 0

Lalit kumar
Lalit kumar

Reputation: 2197

  NSMutableArray *sdf = [(NSDictionary*)[responseString JSONValue]   objectForKey:@"DataTable"];

Check Sdf

   if([sdf isKindOfClass:[NSDictionary class]])
   {
    NSLog(@"Dictionary");
   }

   else if([sdf isKindOfClass:[NSArray class]]) 
   {
    NSLog(@"NSArray");
   }
    else if([sdf isKindOfClass:[NSMutableArray class]]) 
   {
    NSLog(@"NSMutableArray");
   }

Upvotes: 0

CRD
CRD

Reputation: 53000

This is a debugging problem and nobody can really solve it for you as you are using non-local variables whose definition and values are unknown, don't mention that you are using SBJSON (I guess), etc. But let's see if we can give you some pointers. Your error:

[__NSArrayM objectForKey:]: unrecognized selector sent to instance

That tells you that you sent a dictionary method (objectForKey) to an array (__NSArrayM). So somewhere you have an array when you think you have a dictionary.

Now you declare and allocate a dictionary:

NSMutableDictionary * myDict = [[NSMutableDictionary alloc] init];

but then assign to it:

myDict=[sdf objectAtIndex:i];

So this discards the dictionary you allocated and instead assigns whatever is at index i in the array sdf. How do you know, as opposed to think, that the element of the array is a dictionary? You don't test to check...

So where did sdf come from? This line:

NSMutableArray *sdf = [(NSDictionary*)[responseString JSONValue] objectForKey:@"DataTable"];

So that calls JSONValue on some unknown string, assumes the result is a dictionary (could it be an array? or a failure?), looks up a key (did your error come from this line?), and assumes the result is an array.

So what you need to do is go and test all those assumptions, and somewhere you'll find an array where you think you have a dictionary.

Happy hunting!

Upvotes: 3

vaibhav
vaibhav

Reputation: 4096

You do not need to iterate keys and values of dict can directly pass values to array inside else part like:

myArray = [sdf objectForKey:@"RxnCustomerProfile"];

Key RxnCustomerProfile itself containing array not dictionary.


Change your if else part use below code:

if (([(NSString*)sdf isEqual: [NSNull null]])) {

    // Showing AlertView Here
}else {
    myArray = [sdf objectForKey:@"RxnCustomerProfile"];
}

Upvotes: 0

Related Questions