iOS, 'NSInvalidArgumentException' on NSString - ios

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?

Related

exception 'NSInvalidArgumentException', '-[__NSArrayI isEqualToString:]: unrecognized selector sent to instance 0x17a1d170'

I have the above error in my code but I don't know how to solve that please help me for to solve that.
I have give that code.
NSMutableDictionary *dictNewHour = [[NSMutableDictionary alloc]init];
if (_datedetailsArray.count > 0){
for (int i = 0; i < _datedetailsArray.count; i++){
if ([[_datedetailsArray[i] valueForKey:#"totalhours"] isEqualToString:#"4"]) -----> **In this line I have that error**{
NSString *new4Hour = [NSString stringWithFormat:#"%#, %#",[_datedetailsArray[i] valueForKey:#"totalhours"],fourOnly];
[dictNewHour setObject:new4Hour forKey:#"totalhours"];
[fourHoursArray addObject:dictNewHour];
NSLog(#"fourHoursArray is:%#",fourHoursArray);
NSLog(#"fourHoursArray count is:%lu",(unsigned long)fourHoursArray.count);
append4String = [NSString stringWithFormat: #"%# %lu %#", hr4String, (unsigned long)fourHoursArray.count, daysString];
NSLog(#"append4String is:%#",append4String);
}
"isEqualToString" is NSString class method and you are accessing this method with NSArray object So Please check the data

[NSConcreteValue rangeOfString:]: unrecognized selector sent to instance

I try get a range from a string,like:
NSString *str = change[#"new"];
NSLog(#"%#",str);// it will be printed
NSRange range = [str rangeOfString:#"NSPoint"];//the exception uncaughted
NSLog(#"%lu",range.location);
But then i got a exception:
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[NSConcreteValue
rangeOfString:]: unrecognized selector sent to instance
0x7fc258c4f8f0'
And it puzzled me that if i change the strto #"NSPoint: {0, 0}"
It will work?!
Omg, why?
try
NSString *str = [NSString stringWithFormat:#"%#",change[#"new"]];
instead of
NSString *str = change[#"new"];

Unrecognized selector sent to instance On UILabel

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];

iOS - Why does this NSString comparison blow-up? [duplicate]

This question already has an answer here:
Unrecognized selector error for isEqualToString: when setting text of a label
(1 answer)
Closed 8 years ago.
I have looked at SO for similar questions, but am open to being pointed to a duplicate.
I am receiving some JSON from a site, and I want to test for a 404 response.
I have this expression:
NSString *responseString = [json objectForKey:#"statusCode"];
NSLog(#"responseString: %#", responseString);
NSString *myString1 = #"404";
NSLog(#"%d", (responseString == myString1)); //0
NSLog(#"%d", [responseString isEqual:myString1]); //0
NSLog(#"%d", [responseString isEqualToString:myString1]); //Crash
The response string returns 404.
The first and second logs result in 0, and the 3rd crashes with this log:
[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0xb000000000001943
2015-01-29 16:23:33.302 Metro[19057:5064427] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0xb000000000001943'
statusCode is a number, not a string. The error makes this clear by telling you that you are trying to call isEqualToString on an NSNumber.
Try this:
NSInteger responseCode = [json[#"statusCode"] integerValue];
NSInteger notFoundCode = 404;
if (responseCode == notFoundCode) {
// process 404 error
}
The fact that you declared responseString as an NSString does not guarantee that [json objectForKey:#"statusCode"] will indeed return an NSString object.
Actually, the JSON parser detected an integer in your JSON data, and as such, returned an NSNumber. So you should be able to test it against a plain 404 literal using integerValue or, if you want to keep working with strings, will need to convert it first with stringValue.
Anyway, try this, it should return 1:
NSNumber *response = [json objectForKey:#"statusCode"];
...
NSLog(#"%d", [response integerValue] == 404);

iOS app crashing with error: NSCFNumber stringByReplacingOccurrencesOfString:withString: unrecognized selector sent to instance

Our iOS app terminates with an error of [__NSCFNumber stringByReplacingOccurrencesOfString:withString:]: unrecognized selector sent to instance 0x1755e090.
We're testing on an iPhone 5 w iOS 7.
It crashes on this line: [self parseDictionary:notificationMessage intoJSON:jsonStr].
Method containing this line:
- (void)notificationReceived {
NSLog(#"Notification received");
if (notificationMessage && self.callback)
{
NSMutableString *jsonStr = [NSMutableString stringWithString:#"{"];
[self parseDictionary:notificationMessage intoJSON:jsonStr];
if (isInline)
{
[jsonStr appendFormat:#"foreground:\"%d\"", 1];
isInline = NO;
}
else
[jsonStr appendFormat:#"foreground:\"%d\"", 0];
[jsonStr appendString:#"}"];
NSLog(#"Msg: %#", jsonStr);
NSString * jsCallBack = [NSString stringWithFormat:#"%#(%#);", self.callback, jsonStr];
[self.webView stringByEvaluatingJavaScriptFromString:jsCallBack];
self.notificationMessage = nil;
}
}
-(void)parseDictionary:(NSDictionary *)inDictionary intoJSON:(NSMutableString *)jsonString
{
NSArray *keys = [inDictionary allKeys];
NSString *key;
for (key in keys)
{
id thisObject = [inDictionary objectForKey:key];
if ([thisObject isKindOfClass:[NSDictionary class]])
[self parseDictionary:thisObject intoJSON:jsonString];
else
[jsonString appendFormat:#"\"%#\":\"%#\",",
key,
[[[[inDictionary objectForKey:key]
stringByReplacingOccurrencesOfString:#"\\" withString:#"\\\\"]
stringByReplacingOccurrencesOfString:#"\"" withString:#"\\\""]
stringByReplacingOccurrencesOfString:#"\n" withString:#"\\n"]];
}
}
Stack trace (along with one debugging line to show the variable value):
2014-01-07 16:32:36.980 Wopple[195:60b] Notification received
Printing description of self->notificationMessage:
{
"_" = "gHf8EeO3_ZDiugJkgA";
aps = {
alert = "hi foo bar right";
badge = 3;
};
}
2014-01-07 16:33:22.774 Wopple[195:60b] -[__NSCFNumber stringByReplacingOccurrencesOfString:withString:]: unrecognized selector sent to instance 0x1755e090
2014-01-07 16:33:22.776 Wopple[195:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber stringByReplacingOccurrencesOfString:withString:]: unrecognized selector sent to instance 0x1755e090'
*** First throw call stack:
(0x308c3e83 0x3ac246c7 0x308c77b7 0x308c60af 0x30814dc8 0x88da5 0x88d1f 0x88915 0x8757b 0x3335ec93 0x3335f75d 0x340d0b37 0x3088e777 0x3088e713 0x3088cedf 0x307f7471 0x307f7253 0x3552b2eb 0x330ac845 0x7f5d3 0x3b11dab7)
libc++abi.dylib: terminating with uncaught exception of type NSException
When you're using:
[[[[inDictionary objectForKey:key]
stringByReplacingOccurrencesOfString:#"\\" withString:#"\\\\"]
stringByReplacingOccurrencesOfString:#"\"" withString:#"\\\""]
stringByReplacingOccurrencesOfString:#"\n" withString:#"\\n"]];
You're assuming that objectForKey is going to return an NSString object. But, in the case where your app is crashing, the object returned is actually an NSNumber.
You should use isKindOfClass: to determine the object type, or use stringValue on your NSNumber to get a string representation of the object.

Resources