Search code examples
iosobjective-cunrecognized-selector

-[__NSCFNumber objectForKeyedSubscript:]: unrecognized selector sent to instance


So I have been stuck on this for a while now and I have looked through many many posts on Stackoverflow but I still can't seem to figure out how to solve the problem. It seems to me that "dict" is supposed to be converted to a string so I have tried that in many ways, such as using NSString *myString = [NSNumber stringValue];, [NSString stringWithFormat:@"%d", dict[@"id"]]; and a few others. Here is the code:

for (NSDictionary *dict in self.params) {
    NSString *value = dict[@"id"];

    if (value) {
        [parameters appendFormat:@"&ids[]=%@", value];
    }
}

The error I get is on this line: NSString *value = dict[@"id"];

The output on the console shows:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber objectForKeyedSubscript:]: unrecognized selector sent to instance 0xb00000000000a313'

I think the problem is that it does not seem to convert to a string properly. The line shows dict as an NSCFNumber (long)2609. How can I convert it to a string?


Solution

  • // try like this it will work

    for (NSDictionary *dict in self.params) {
    if([dict isKindOfClass:[NSDictionary class]]) {
      NSString *value = [NSString stringWithFormat:@"%@",dict[@"id"]];
    
      if (value) {
          [parameters appendFormat:@"&ids[]=%@", value];
      }
    }
    }