__NSCFString objectForKeyedSubscript: exception - ios

I take datas from server. My app work fine in Sinulator and test device iPhone 4s, but one man have problem on iPod 4. He get exception:
-[__NSCFString objectForKeyedSubscript:]: unrecognized selector sent to instance 0x1d263a20
I cann't use this device so I write code to know where crash was.
if (![dictionaryRest[#"compliments"] isEqual:[NSNull null]]) {
NSMutableArray *array = [NSMutableArray new];
NSMutableArray *firstArray = [NSMutableArray new];
for (NSDictionary *dic in dictionaryRest[#"compliments"]) {
Compliment *compl = [Compliment new];
if (![dic[#"ID_promotions"] isEqual:[NSNull null]])
compl.ID = [dic[#"ID_promotions"] integerValue];
So in last 2 strings this exception was. What the reason of this? So I understand that I need use
if ([dict objectForKey:[#"compliments"])
instead
if (![dict[#"compliments"] isEqual:[NSNull null]])
and in all another cases.
I test now and I have in my dictionary for ID:

You have an NSString instance in your dictionary where you expect a dictionary.
Note that your "use this instead of that" has nothing to do with the problem.

-objectForKeyedSubscript: is an instance method on NSDictionary object.
__NSCFString objectForKeyedSubscript: exception indicates that the method -objectForKeyedSubscript is somehow getting called on some NSString object.
So basically you just have to properly check for the class of the object before you can safely assume that it is actually dictionary.
if([dic isKindOfClass:[NSDictionary class]])
{
id obj = dic[#"key"];
}

Related

-[__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'
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!
YOU FETCH THE VALUE IN ARRAY FORMAT AND YOU INTEGRATE METHOD IN DICTIONARY.
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"];
}
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");
}
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.

NSInvalidArgumentException', reason: '-[__NSArrayI length]: unrecognized selector sent to instance 0x165d5150'

Hi I am getting this data form server
NSDictionary*feed=[saveDic objectForKey:#"feed"];
NSLog(#"%#",feed); //Outputs: feed = ( { code = yQ7j0t; "user_id" = 889445341091863; } ); }
NSLog(#"%#",[feed valueForKey:#"code"]);
NSString *referralCode = [feed valueForKey:#"code"];
NSLog(#"%#",referralCode);
self.referralCode.text=referralCode;
And beacuse of that I am getting below error.
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI length]: selector sent to instance 0x165d5150'``
Any help will be appreciated.
The issue is, your feed key holds an array. You are not properly handling that in your code, that is why the crash occurs. When you call valueForKey: it retrieves an array of values held by that specific key.
For fixing that you can use:
NSArray *feed = [saveDic objectForKey:#"feed"];
NSArray *referralCodes = [feed valueForKey:#"code"];
NSString *referralCode = referralCodes.count ? referralCodes[0] : #"";
NSLog(#"%#",referralCode);
But I personally prefer using objectForKey: instead of valueForKey:. So you can re-write the code like:
NSArray *feed = [saveDic objectForKey:#"feed"];
NSString *referralCode = feed.count ? [feed[0] objectForKey:#"code"] : #"";
NSLog(#"%#",referralCode);
Some where you use a variable;
yourVaribleName.length
or
[yourVaribleName length]
which should be
yourVaribleName.count
note: the crash says exactly that "yourVaribleName" is NSArray type where you wants length of the NSArray. But NSArray has not feature "length". NSArray has "Count" feature
//try with this code bellow
NSArray *referralCode = [feed valueForKey:#"code"];
NSLog(#"%#",referralCode);
self.referralCode.text=[referralCode componentsJoinedByString:#" "];//#"," or #"" what event you need
Your feed data is in array. So you have retrieve code value from array.Hope it will help you.
NSMutableArray*feed=[saveDic objectForKey:#"feed"];
NSLog(#"%#",feed);
NSLog(#"%#",[feed valueForKey:#"code"]);
NSString *referralCode = [[feed objectAtIndex:indexPath]valueForKey:#"code"];
NSLog(#"%#",referralCode);
self.referralCode.text=referralCode;

Unrecognized Selector on Calling allKeys

I'm calling the following code on a number of JSON dictionaries within another dictionary (so in this case, NSDictionary objects with another NSDictionary.
-(NSString *)getAllDictionaryValues:(NSDictionary *)dict
{
NSString *output=[NSString stringWithFormat:#""];
for(NSString *key in [dict allKeys])
{
output = [NSString stringWithFormat:#"%#\n%#: %#",output, key, [dict objectForKey:key]];
}
return output;
}
When I run this code, however, I get
2014-03-30 01:27:35.565 WebServiceTest[48606:60b] -[__NSCFString allKeys]: unrecognized selector sent to instance 0x9270230
What is wrong about my call to allKeys?
Like when you sending message to this function it may be declare your code that dict is an NSDicationay object (when you call this function) but it not an dictionary may be an array of String or etc that why you getting the exception.
-(NSString *)getAllDictionaryValues:(NSDictionary *)dict
So first check your dict like
if([dict isKindOfClass:[NSArray class]]){
//is an array
}else if([dict isKindOfClass:[NSDictionary class]]){
//is an dictionary
}else if([dict isKindOfClass:[NSString class]]){
//is an string
}else{
//or its something else
}
May be you are inputting a string rather than a dictionary object in the calling method
That's obvious. As log said, you tried to call allKeys method on __NSCFString instance. It means that you sent NSString instead of NSDictionary to getAllDictionaryValues: method.
It means the dict object is NSString type. You can check the object like this:
if([dict isKindOfClass:[NSDictionary class]]) {
// dict is NSDictionary type of object
} else if([dict isKindOfClass:[NSString class]]) {
// dict is string type of object.
}

[__NSCFString count]: unrecognized selector sent to instance 0x6a7a1a0 while taking datas from json string

- (void)viewDidLoad
{
binding.logXMLInOut = YES; // to get logging to the console.
StationDetailsJsonSvc_getAvailableStations *request = [[StationDetailsJsonSvc_getAvailableStations new] autorelease];
request.userName=#"twinkle";
request.password=#"twinkle";
StationDetailsJsonSoap11BindingResponse *resp = [binding getAvailableStationsUsingParameters:request];
for (id mine in resp.bodyParts)
{
if ([mine isKindOfClass:[StationDetailsJsonSvc_getAvailableStationsResponse class]])
{
resultsring = [mine return_];
NSLog(#"list string is%#",resultsring);
}
}
#pragma mark parsing
SBJsonParser *parserq = [[SBJsonParser alloc] init];
//if successful, i can have a look inside parsedJSON - its worked as an NSdictionary and NSArray
results= [parserq objectWithString:resultsring error:nil];
NSLog(#"print %#",results);
for (status in results)
{
NSLog(#"%# ",[status objectForKey:#"1" ]);
events=[status objectForKey:#"1"];
NSLog(#"get%#",events);
NSLog(#"events%#",events);
}
events=[status objectForKey:#"1"];
NSLog(#"post%#",events);
self.navigationController.navigationBarHidden=YES;
[whethertableview reloadData];
[super viewDidLoad];
}
this is my code am not getting tableview contents if i run my app crashes getting [NSCFString count]:unrecognized selector sent to instance
You should not get count on NSString but on arrays
you should call [yourString length] to check if the string has something.
You are trying to get the count of a string , which is crashing the App
There are various improvements you could make with this code, but I think I see the problem:
As you are not using ARC, you need to retain what you take out of the parser:
So instead of:
events=[status objectForKey:#"1"]
You need to do:
events= [[status objectForKey:#"1"] retain];
Your crash is caused by accessing a variable that has already been released. More than likely it is the events variable.
...and to add to this. events is probably an NSArray which 'count' is being called on. And [status objectForKey:#"1"] is returning a string... there are many possibilities which i'm speculating about. If events is an NSArray, this isn't the way to add objects to the array.. you repeatedly call events=[status objectForKey:#"1"]; in a loop too.

[iOS][objC] unable to convert value in NSDictionary to NSString

I intend to convert the NSDictionary* object in iOS SDK to NSString*.
Lets say my NSDictionary object has following key value pairs:
{"aps":{"badge":9, "alert":"hello"}} (notice that value itself is a NSDictionary object)
and I want it to convert into a hash map with key value pair as {"aps":"badge:9, alert:hello"} (notice value is just a string).
I am able to print the value in NsDictionary using the following code:
NSDictionary *userInfo; //it is passed as an argument and contains the string I mentioned above
for (id key in userInfo)
{
NSString* value = [userInfo valueForKey:key];
funct( [value UTF9String]; // my function
}
But i am not able to call any NSString method on value object like UTT8String. It gives me error "Terminating app due to uncaught exception NSInvalidArgumentException: reason [_NSCFDictionary UTF8String]: unrecognised selector sent to instance
You are going to have to recursively process the dictionary structure, here is an example that you should be able to adapt:
-(void)processParsedObject:(id)object{
[self processParsedObject:object depth:0 parent:nil];
}
-(void)processParsedObject:(id)object depth:(int)depth parent:(id)parent{
if([object isKindOfClass:[NSDictionary class]]){
for(NSString * key in [object allKeys]){
id child = [object objectForKey:key];
[self processParsedObject:child depth:depth+1 parent:object];
}
}else if([object isKindOfClass:[NSArray class]]){
for(id child in object){
[self processParsedObject:child depth:depth+1 parent:object];
}
}
else{
//This object is not a container you might be interested in it's value
NSLog(#"Node: %# depth: %d",[object description],depth);
}
}
You need to apply that loop to each child, not the main dictionary. You said yourself you have a dictionary in a dictionary:
for(id key in userInfo)
{
NSDictionary *subDict = [userInfo valueForKey:key];
for(id subKey in subDict)
{
NSString* value = [subDict valueForKey:subKey];
}
}
This loop assumes you have the entire dictionary in the first level, otherwise you will need to use danielbeard's recursive method.
I found the easiest way out. calling the description method on the NSDictionary object gives me what I needed exactly. Stupid to have missed it on first go.

Resources