I am parsing JSON and get the value.Store value as NSString and when I want to show on UILabel app crashes with
unrecognized selector sent to instance 0x7b070470`
NSString *idvalue = [jsonResponse objectForKey:#"id"];`
labl_id.text =idvalue;
NSLog(#"id value%#",idvalue);
output: id value 6
please let me know how to fix this
thanks
mb you get NSNumber
NSString *idvalue = [[jsonResponse valueForKey:#"id"] stringValue];
Related
I have data coming in from a JSON source.
The value of key hour is an integer:
{"busy": {"day":"monday", "month":"February", "hour":20}}
Here i make use of the values
NSString *hour = [[self.stats valueForKey:#"busy"] valueForKey:#"hour"];
NSString *day = [[self.stats valueForKey:#"busy"] valueForKey:#"day"];
NSString *month = [[self.stats valueForKey:#"busy"] valueForKey:#"month"];
NSLog(#"hour: %#", hour);
NSLog(#"day: %#", day);
NSLog(#"month: %#", month);
self.busyHour.text = (NSString *)hour;
self.busyDay.text = day;
self.busyMonth.text = month;
I get the correct output from the NSLogs of all 3 of the values, yet when i attempt to assign self.busyHour.text i get the following error.
-[__NSCFNumber length]: unrecognized selector sent to instance 0xb000000000000093
2015-02-16 10:30:41.133 changeView[1494:411656] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber length]
NSInteger hour = [[[self.stats valueForKey:#"busy"] valueForKey:#"hour"] integerValue];
self.busyHour.text = [NSString stringWithFormat:#"%i", hour];
This is really self explanatory... you're assigning hour to self.busyHour and you're casting it - did you check it's really an NSString before doing so?
In my previous app I had this code to extract JSON from an NSDictionary variable into a string:
NSString *task_id = [jsonString objectForKey:#"key"];
It worked very well but for some reason it won't work in my new app anymore? Instead I get this error:
-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x14d7bda0
Does anyone know why this might not work?
Peter
Make sure that the jsonString is NSDictionary:
if([jsonString isKindOfClass:[NSDictionary class]]) {
NSString *task_id = [jsonString objectForKey:#"key"];
} else {
// is not a dictionary
NSLog(#"%#", jsonString);
}
I have a table view which may contain number of data.
When user select one particular cell I set Id in string based on that selection. And then I am trying to find length of that string. But I am getting error in that. When I print that string it print its value.
Here is my code.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSArray *charityArray = [dataArray objectAtIndex:indexPath.row];
charityId=[charityArray objectAtIndex:0];
NSLog(#"charityId%#",charityId);
NSLog(#"charitylen%d",[charityId length]);
}
and here is log screen.
2013-12-04 14:47:13.897 GratZeez[2178:a0b] charityId3
2013-12-04 14:47:13.897 GratZeez[2178:a0b] -[__NSCFNumber length]: unrecognized selector sent to instance 0xd174070 2013-12-04 14:47:13.899
GratZeez[2178:a0b] * Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[__NSCFNumber length]:
unrecognized selector sent to instance 0xd174070'
declaration of charityId:
#import <UIKit/UIKit.h>
#interface ExsitingCharityViewController : UIViewController<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
{
NSMutableArray *dataArray,*tempArray,*searchResultArray,*nameSearchArray;
BOOL search;
NSMutableDictionary *dataDict;
CGPoint originalCenter;
NSString *charityId;
}
try this code, Convert NSNumber to NSString and then use length
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSArray *charityArray = [dataArray objectAtIndex:indexPath.row];
charityId = [charityArray objectAtIndex:0];
NSString *charityString = [charityId stringValue];
NSLog(#"charityString = %#", charityString);
NSLog(#"charityString length = %d",[charityString length]);
}
As you post, charityId s not a NSNumber formatted type value.
If you want to know to length convert in to NSString formate,
Like, NSString* str = [NSString stringwithFormat:#"%#", charityId];
then you get length.
As the error log shows, you are sending the length message to a NSNumber object. According to your post, you seem to be trying to send it to a NSString but it is not a NSString.
-[__NSCFNumber length]: unrecognized selector sent to instance 0xd174070'
If you wish to use the %d string format specifier with an NSNumber you should convert the NSNumber object to an integer first:
NSLog(#"charityId%d",[charityId intValue]);
use this code for NSNumber to NSString
NSString *strId=[NSString StringWithFormat#"%d", charityId];
NSLog(#"charitylen%d",[strId length]);
If you are trying to NSLog an NSNumber do the following, replace this:
NSLog(#"charitylen%d",[charityId length]);
With this:
NSString *myString = [charityId stringValue];
NSLog(#"charitylen%#", myString);
And if you are trying to get the count of your Array use the following:
NSLog(#"charitylen%d",[charityId count]);
Update:
I have to opinions about your case:
1- It means that you are passing an NSNumber where the called code expects an NSString or some other object that has a length method. You can tell Xcode to break on exceptions so that you see where exactly the length method gets called.
2-I think it may because your charityId is empty or never used before you NSLog it .
-[__NSCFNumber length]: unrecognized selector sent to instance it's mean you are tried to get length of a number (i.e. int, NSInteger etc). so you need to typecast.
you can do it either using [NSString stringWithFormat:#"%d", charityId] or [charityId stringValue]
so just do below
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSArray *charityArray = [dataArray objectAtIndex:indexPath.row];
charityId=[charityArray objectAtIndex:0];
NSString *charityIdString=[NSString stringWithFormat:#"%d", charityId];
NSLog(#"charityId%#",charityIdString);
NSLog(#"charitylen%d",[charityIdString length]);
}
I have JSON on my server, which is parsed into iOS app to NSDictionary. NSDictionary looks like this:
(
{
text = Aaa;
title = 1;
},
{
text = Bbb;
title = 2;
}
)
My question is - how to get just text from first dimension, so it should be "Aaa". I've tried to use this:
[[[json allValues]objectAtIndex:0]objectAtIndex:0];
But it didn't work, it ends with error
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[__NSCFArray allValues]:
unrecognized selector sent to instance 0x714a050'
So can you help me please, how to get just one value from specified index? Thanks!
That error message is simply telling you that NSDictionary (which is the first object of that array, along with the second) doesn't respond to objectAtIndex.
This will be a bit cody, but it explains it better:
NSArray *jsonArray = [json allValues];
NSDictionary *firstObjectDict = [jsonArray objectAtIndex:0];
NSString *myValue = [firstObjectDict valueForKey:#"text"];
Your JSON object is an array, containing two dictionaries. That's how to get the values:
NSDictionary* dict1= json[0];
NSString* text= dict1[#"text"];
NSString* title= dict1[#"title"];
Try this:
NSString *txt = [[json objectAtIndex:0] objectForKey:#"text"];
UPDATE: Have fixed the error. Thanks yunas.
I was passed NSstring it is coming properly data into uitableview but when I am parsing integer value it showing exception Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: -[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x744e9d0
This is my code
(void)fetchedData:(NSData *)responseData {
//parse out the json data
NSError* error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:responseData options:kNilOptions error:&error];
NSArray * values=[json objectForKey:#"loans"];
NSLog(#"Array: %#",values);
for (NSDictionary *file in values)
{
NSNumber *fileTitle=[file objectForKey:#"id"];
// NSString *fileTitle = [file objectForKey:#"sector"];
[titles addObject: fileTitle];
}
[self.mainTableView reloadData];
}
You are assigning string value to NSNumber type.
Try to use this
NSString *fileTitle=[file objectForKey:#"id"];
[titles addObject: fileTitle];
and use string value.
[[titles addObject: fileTitle] stringValue];
I guess you are doing something like this in your -tableView: cellForIndexPath:
UITableViewCell *cell = [...];
cell.titleLabel.text = titles[indexPath.row];
You can only use NSString for -[UILabel setText:] method.
Convert your NSNumber to NSString with a simple -[NSNumber stringValue] or using NSNumberFormatter.
Everything seems ok. Just replace
NSNumber *fileTitle=[file objectForKey:#"id"];
by NSString *fileTitle = [file objectForKey:#"id"];