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
Related
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];
This question already has answers here:
How can I debug 'unrecognized selector sent to instance' error
(9 answers)
Closed 8 years ago.
I'm trying to filter with searchController. I am getting a crash (-[__NSArrayI length]: unrecognized selector sent to instance) at line NSRange productNameRange = NSMakeRange(0, [[_list valueForKey:#"name"] length]); in the following code:
-(void)updateFilteredContent:(NSString*)searchText {
// Update the filtered array based on the search text and scope.
[self.searchResults removeAllObjects];
// Filter the array using NSPredicate
NSLog(#"_categories are %#",_categories);
for (NSDictionary *object in _categories) {
NSUInteger searchOptions = NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch;
NSRange productNameRange = NSMakeRange(0, [[_list valueForKey:#"name"] length]);
NSRange foundRange = [[_list valueForKey:#"name"] rangeOfString:searchText options:searchOptions range:productNameRange];
if (foundRange.length > 0) {
[self.searchResults addObject:object];
}
}
}
I've tried adding an if statement on the length like if ([[object valueForKey:#"name"] length] >0){ but it still crashing.
an example of the log for _list is:
_list names are (
"Honda",
"Ford",
Toyota,
your [_list valueForKey:#"name"] returns an NSarray, which should be an NSString to calculate length of it..
Please check with this condition and let me know if it still crashes!
if([_list allkeys] contains #"name"])
{
NSString *tempStr = [_list valueForKey:#"name"];
}
if(tempStr)
NSRange productNameRange = NSMakeRange(0, [tempStr length]);
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.
The following code
NSMutableString *a = [[NSMutableString alloc] init];
NSMutableString *pivot =[[NSMutableString alloc] init];
int i= 0;
for ( i = 0; i < [columns count]; i++)
{
if ([[[data objectForKey:pivot] class] isKindOfClass:[NSNumber class]])
{
sqlite3_bind_int(sqlStatement, i+1,[[columns objectAtIndex:i] integerValue]);
}
else
{
// NSLog(#"kind of class %# %#",NSStringFromClass([[data objectForKey:pivot] class]) ,[data objectForKey:pivot]);
[pivot setString:[columns objectAtIndex:i]];
[a setString:(NSString*)[data objectForKey:pivot]];
sqlite3_bind_text(sqlStatement, i+1, [a UTF8String], -1, 0);
}
}
a = nil;
pivot = nil;
raises an exception
kind of class __NSCFConstantString
-[__NSCFNumber length]: unrecognized selector sent to instance 0xc94c240
But the log clearly says that [data objectForKey:pivot] is a String! And the test variable is always 0!
I checked in the debug and actually [data objectForKey:pivot] is a Number. And in fact I use an if (not shown) to check if it is a number or not. Obviously, the if fails...
You are casting the class to a string when you try to format it this way, try to get the string name from the class with NSStringFromClass(class) :
id pivotObject = [data objectForKey:pivot];
NSLog(#"kind of class %#", NSStringFromClass([pivotObject class]));
if ([pivotObject isKindOfClass:[NSString class]]) {
[pivot setString:[columns objectAtIndex:i]];
[a setString:pivotObject];
}
Also the isKindOfClass: returns a BOOL not int.
But most importantly:
I think the error in the [pivot setString:[columns objectAtIndex:i]] line, since here you change the pivot variable and then retrieve it from data, But since the pivot variable is changed you request a different key from the data dictionary. This you will get an other value. My code I only grab the pivotData once from data.
Hello i get a json that looks like this:
features: (
{
attributes = {
Gecontroleerd = Ja;
};
geometry = {
x = "5.968097965285907";
y = "52.50707112779077";
};
}
)
From this code:
NSDictionary *root = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
NSArray *data = [root objectForKey:#"features"];
NSLog(#"features: %#", data );
for (NSArray *row in data) {
NSString *latitude = row[5];
NSString *longitude = row[7];
NSString *crimeDescription = #"test";
NSString *address = #"banaan";
And u need to x values 5.968097965285907 for latitude
and y values 52.50707112779077 for longitude
But i get this error:
[__NSCFDictionary objectAtIndexedSubscript:]: unrecognized selector sent to instance 0x14831450
2012-11-14 10:10:59.000 ArrestPlotter[6330:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndexedSubscript:]: unrecognized selector sent to instance 0x14831450'
*** First throw call stack:
(0x1860012 0x1655e7e 0x18eb4bd 0x184fbbc 0x184f94e 0x2dbc 0x3b8f 0x1cc1e 0x16696b0 0xfa0035 0x17e3f3f 0x17e396f 0x1806734 0x1805f44 0x1805e1b 0x224a7e3 0x224a668 0x4a765c 0x25bd 0x24e5 0x1)
libc++abi.dylib: terminate called throwing an exception
(lldb)
Does anyone wich row i need to select?
I guess that the only thing is that the row number needs to be changed. Or maybe there should be something like this : [1][5]. Im not quite sure how this works
NSArray *data = [root objectForKey:#"features"];
NSLog(#"features: %#", data );
for (NSDictionary *dic in data) {
NSDictionary geometry = [dic objectForKey:#"geometry"];
// Do what you want..
NSString *myAwesomeX = [geometry objectForKey:#"x"];
NSString *myAwesomeY = [geometry objectForKey:#"y"];
}
The problem here is that you are trying to send a selector message to object row, that is in memory a NSDictionary (NSCFDictionary?) object, and you are trying to manage it like a NSArray.
The method objectAtIndexedSubscript (is underlying called by row[5] and row[7]) exists in NSDictionary, but no in NSArray.
Change
for (NSArray *row in data) {
by
for (NSDictionary *row in data) {
Also, you have to change the management of data inside for, look at the result of your log statement and act accord whit it.
I hope this will help!