Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ITCreative

macrumors member
Original poster
in my code there is exception:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString objectForKey:]: unrecognized selector sent to instance 0x6133a70'

this is my code


Code:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[buffer setLength:0];}

  - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
 [buffer appendData:data];}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSString *str = [[[NSString alloc] initWithData:buffer encoding:NSUTF8StringEncoding] autorelease];

    NSArray *array = [str JSONValue];

    if (!array)
        return;

    NSDateFormatter *fmt = [[[NSDateFormatter alloc] init] autorelease];
    [fmt setDateFormat:@"yyyy-MM-dd"];

    for (NSDictionary *dict in array) {
        NSLog(@"Class of eve_date = %@", [[dict objectForKey:@"eve_date"]class]);

        NSDate *d = [fmt dateFromString:[dict objectForKey:@"eve_date"]];

        NSLog(@"%@",d);
        [eventPHP addObject:[Events eventsNamed:[dict objectForKey:@"title_event"] description:[dict objectForKey:@"description"] date:d]];
    }
}

This is my data

{"event":[{"eve_date":"2011-12-24","eve_time":"1 pm","title":"Tooth Regeneration Research","decription":"Tooth Regeneration Research:Where Do we stand today?\r\n\r\nBy: Dr.Tarek H El-Bialy,phD,FRCD(c)\r\n\r\nvenue:VIP ROOM,college of Dentistry,KSU"},{"eve_date":"2011-12-21","eve_time":"8 am","title":"The First Knowledge Translation(KT)","decription":"The First Knowledge Translation(KT)symposium in saudi Arabia\r\n\r\nvenue: main Auditorium,college of medicine\r\n\r\nFor Registration and further inquiries please concat:\r\n\r\nTel:4690790\/Email:ebhc-kt@ksu.edu.sa"}]}

plz help me to solve it i search to find way to solve it but didn't found result. 🙁
 
Are you sure array is an array ? Are you sure dict is a dictionary ? The exception seems to point to dict being a NSCFString, not a NSDictionary. That is why it's not responding to objectForKey: which is a selector in the NSDictionary class.

Reading through the JSON API, it states the following :

Code:
@interface NSString (NSString_SBJsonParsing)

/**
 @brief Decodes the receiver's JSON text
 
 @return the NSDictionary or NSArray represented by the receiver, or nil on error.
 
 @see @ref json2objc
 */
- (id)JSONValue;

Maybe you want to make sure it's returning what you think it's returning, ie, an array. Look into NSObject's isKindofClass: and go from there (or use the debugger to inspect the objects being returned in your function).

Seems to me your first call to [str JSONValue] is returning an NSDictionary and your for is then looping not through an array, but through the key/value pairs of the dictionary itself, which are probably all NSString objects that do not respond to objectForKey:.
 
Are you sure array is an array ? Are you sure dict is a dictionary ? The exception seems to point to dict being a NSCFString, not a NSDictionary. That is why it's not responding to objectForKey: which is a selector in the NSDictionary class.

Reading through the JSON API, it states the following :

Code:
@interface NSString (NSString_SBJsonParsing)

/**
 @brief Decodes the receiver's JSON text
 
 @return the NSDictionary or NSArray represented by the receiver, or nil on error.
 
 @see @ref json2objc
 */
- (id)JSONValue;

Maybe you want to make sure it's returning what you think it's returning, ie, an array. Look into NSObject's isKindofClass: and go from there (or use the debugger to inspect the objects being returned in your function).

Seems to me your first call to [str JSONValue] is returning an NSDictionary and your for is then looping not through an array, but through the key/value pairs of the dictionary itself, which are probably all NSString objects that do not respond to objectForKey:.


I try print " str " & " array to make sure receive data this is result:


str :

2011-12-22 01:35:52.506 iKSU[4318:207] str: {"event":[{"eve_date":"2011-12-24","eve_time":"1 pm","title":"Tooth Regeneration Research","decription":"Tooth Regeneration Research:Where Do we stand today?\r\n\r\nBy: Dr.Tarek H El-Bialy,phD,FRCD(c)\r\n\r\nvenue:VIP ROOM,college of Dentistry,KSU"},{"eve_date":"2011-12-21","eve_time":"8 am","title":"The First Knowledge Translation(KT)","decription":"The First Knowledge Translation(KT)symposium in saudi Arabia\r\n\r\nvenue: main Auditorium,college of medicine\r\n\r\nFor Registration and further inquiries please concat:\r\n\r\nTel:4690790\/Email:ebhc-kt@ksu.edu.sa"}]}

then print array :

2011-12-22 01:35:52.507 iKSU[4318:207] Array: {
event = (
{
decription = "Tooth Regeneration Research:Where Do we stand today?
\n
\nBy: Dr.Tarek H El-Bialy,phD,FRCD(c)
\n
\nvenue:VIP ROOM,college of Dentistry,KSU";
"eve_date" = "2011-12-24";
"eve_time" = "1 pm";
title = "Tooth Regeneration Research";
},
{
decription = "The First Knowledge Translation(KT)symposium in saudi Arabia
\n
\nvenue: main Auditorium,college of medicine
\n
\nFor Registration and further inquiries please concat:
\n
\nTel:4690790/Email:ebhc-kt@ksu.edu.sa";
"eve_date" = "2011-12-21";
"eve_time" = "8 am";
title = "The First Knowledge Translation(KT)";
}
);
}

the exception happen


2011-12-22 01:35:52.506 iKSU[4318:207] str: {"event":[{"eve_date":"2011-12-24","eve_time":"1 pm","title":"Tooth Regeneration Research","decription":"Tooth Regeneration Research:Where Do we stand today?\r\n\r\nBy: Dr.Tarek H El-Bialy,phD,FRCD(c)\r\n\r\nvenue:VIP ROOM,college of Dentistry,KSU"},{"eve_date":"2011-12-21","eve_time":"8 am","title":"The First Knowledge Translation(KT)","decription":"The First Knowledge Translation(KT)symposium in saudi Arabia\r\n\r\nvenue: main Auditorium,college of medicine\r\n\r\nFor Registration and further inquiries please concat:\r\n\r\nTel:4690790\/Email:ebhc-kt@ksu.edu.sa"}]}
2011-12-22 01:35:52.507 iKSU[4318:207] Array: {
event = (
{
decription = "Tooth Regeneration Research:Where Do we stand today?
\n
\nBy: Dr.Tarek H El-Bialy,phD,FRCD(c)
\n
\nvenue:VIP ROOM,college of Dentistry,KSU";
"eve_date" = "2011-12-24";
"eve_time" = "1 pm";
title = "Tooth Regeneration Research";
},
{
decription = "The First Knowledge Translation(KT)symposium in saudi Arabia
\n
\nvenue: main Auditorium,college of medicine
\n
\nFor Registration and further inquiries please concat:
\n
\nTel:4690790/Email:ebhc-kt@ksu.edu.sa";
"eve_date" = "2011-12-21";
"eve_time" = "8 am";
title = "The First Knowledge Translation(KT)";
}
);
}
2011-12-22 01:35:52.508 iKSU[4318:207] -[NSCFString objectForKey:]: unrecognized selector sent to instance 0x64180d0
2011-12-22 01:35:52.511 iKSU[4318:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString objectForKey:]: unrecognized selector sent to instance 0x64180d0'
 
You need to better understand your JSON structures. { is the start of an object, i.e. a dictionary with these tools. [ is the start of an array. Your string starts with a dictionary.
 
Are you sure array is an array ? Are you sure dict is a dictionary ? The exception seems to point to dict being a NSCFString, not a NSDictionary. That is why it's not responding to objectForKey: which is a selector in the NSDictionary class.

Reading through the JSON API, it states the following :

Code:
@interface NSString (NSString_SBJsonParsing)

/**
 @brief Decodes the receiver's JSON text
 
 @return the NSDictionary or NSArray represented by the receiver, or nil on error.
 
 @see @ref json2objc
 */
- (id)JSONValue;

Maybe you want to make sure it's returning what you think it's returning, ie, an array. Look into NSObject's isKindofClass: and go from there (or use the debugger to inspect the objects being returned in your function).

Seems to me your first call to [str JSONValue] is returning an NSDictionary and your for is then looping not through an array, but through the key/value pairs of the dictionary itself, which are probably all NSString objects that do not respond to objectForKey:.



I try use isKindOfClass

Code:
NSDictionary *dict = [str JSONValue];
	
	id myObject = [dict valueForKeyPath:@"event"];
	if ([myObject isKindOfClass:[NSDictionary class]]) {
	NSArray *array = [NSArray arrayWithObject:myObject];
		NSLog(@"YES %@",array);
	} else {
	NSArray *array = myObject;
		NSLog(@"NO %@",array);

	}

it is print NO




You need to better understand your JSON structures. { is the start of an object, i.e. a dictionary with these tools. [ is the start of an array. Your string starts with a dictionary.


ok thanx 😱, i try solve it.
 
I try use isKindOfClass

Code:
NSDictionary *dict = [str JSONValue];
	
	id myObject = [dict valueForKeyPath:@"event"];
	if ([myObject isKindOfClass:[NSDictionary class]]) {
	NSArray *array = [NSArray arrayWithObject:myObject];
		NSLog(@"YES %@",array);
	} else {
	NSArray *array = myObject;
		NSLog(@"NO %@",array);

	}

it is print NO

What is [dict valueForKeyPath😡"event"]; and what type of object does that return ? You changed the code completely, you weren't using this valueForKeyPath: selector before. valueForKeyPath: can return more than 1 object and thus it could return either a Dictionary, an Array or something else entirely seeing how it returns an object. Are you sure your call with the string @"event" is going to return what you think it's going to return ?

Did you try simply implementing isKindOfClass in your original code ? Did you look at the object types returned through the debugger ? I think before changing anything else, you should just fix your broken initial code with our suggestions, then look at making changes/modifications.
 
I change my code to :


Code:
NSString *str = [[[NSString alloc] initWithData:buffer encoding:NSUTF8StringEncoding] autorelease];
	NSLog(@"str: %@",str);
	
	NSDictionary *dict = [str JSONValue];
	
	NSDateFormatter *fmt = [[[NSDateFormatter alloc] init] autorelease];
	[fmt setDateFormat:@"yyyy-MM-dd"];
	
 	
		
	NSArray *array = [[dict objectForKey:@"event"] retain];
		
	NSLog(@"Array: %@",array);
	
		
	
	for (NSDictionary *tempdict in array)
	{
		NSDate *d = [fmt dateFromString:[tempdict objectForKey:@"eve_date"]];
		NSLog(@"Date %@",d);
		NSLog(@"Date of event %@",[tempdict objectForKey:@"eve_date"]);

		NSLog(@"This is title_event %@",[tempdict objectForKey:@"title"]);
		NSLog (@"Time of event %@", [tempdict objectForKey:@"eve_time"]);
		NSLog(@"This is description %@",[tempdict objectForKey:@"description"]);
		
	[eventPHP addObject:[Events eventsNamed:[tempdict objectForKey:@"title"] description:[tempdict objectForKey:@"description"] date:[tempdict objectForKey:@"eve_date"]]];
		
		
		
		
	}


it work fine its print every thing but in this line :

Code:
[eventPHP addObject:[Events eventsNamed:[tempdict objectForKey:@"title"] description:[tempdict objectForKey:@"description"] date:[tempdict objectForKey:@"eve_date"]]];

there is exception

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDate length]: unrecognized selector sent to instance 0x6456a80'

how solve this exception ?
 
Code:
[eventPHP addObject:[Events eventsNamed:[tempdict objectForKey:@"title"] description:[tempdict objectForKey:@"description"] date:[tempdict objectForKey:@"eve_date"]]];

there is exception



how solve this exception ?

You'll need to help us a bit. What is Events ? What is the eventsNamed method's argument types ?

Finally, are you sure [tempdict objectForKey: @"eve_date"] returns an NSDate object ? Your eventsNamed method probably wants an NSDate for the date: argument but your dictionary contains something else for the key eve_date.
 
You'll need to help us a bit. What is Events ? What is the eventsNamed method's argument types ?


Code:
+ (Events*)eventsNamed:(NSString *)atitle description:(NSString *)adescription date:(NSDate *)aDate
{

	return [[[Events alloc] initWithName:atitle description:adescription date:aDate] autorelease];
}


Finally, are you sure [tempdict objectForKey: @"eve_date"] returns an NSDate object ? Your eventsNamed method probably wants an NSDate for the date: argument but your dictionary contains something else for the key eve_date.


ya i test [tempdict objectForKey: @"eve_date"] it is print date 😕
 
Code:
+ (Events*)eventsNamed:(NSString *)atitle description:(NSString *)adescription date:(NSDate *)aDate
{

	return [[[Events alloc] initWithName:atitle description:adescription date:aDate] autorelease];
}





ya i test [tempdict objectForKey: @"eve_date"] it is print date 😕

Ok, looking further into it, NSDate does not have a length method or property. What is Events and what does its initWithName procedure do with the date ? If it alls [aDate length] at any point, that is what is causing your exception.

It's basically the same error you got initially in this post, you're calling a method/property on a class that doesn't have this method or property.

You probably have a warning for it, Xcode gives me one here :

Code:
main.m: warning: Semantic Issue: 'NSDate' may not respond to 'length'
 
Ok, looking further into it, NSDate does not have a length method or property. What is Events and what does its initWithName procedure do with the date ? If it alls [aDate length] at any point, that is what is causing your exception.

It's basically the same error you got initially in this post, you're calling a method/property on a class that doesn't have this method or property.

You probably have a warning for it, Xcode gives me one here :

Code:
main.m: warning: Semantic Issue: 'NSDate' may not respond to 'length'

Events its class contain some property

and this is initWithName

Code:
- (id)initWithName:(NSString *)atitle description:(NSString *)adescription date:(NSDate *)aDate
{
	

	if ((self = [super init])) {
		title = [atitle copy];
		description = [adescription copy];
		date = [aDate retain];
	}
	return self;
	
}
 
Events its class contain some property

and this is initWithName

Code:
- (id)initWithName:(NSString *)atitle description:(NSString *)adescription date:(NSDate *)aDate
{
	

	if ((self = [super init])) {
		title = [atitle copy];
		description = [adescription copy];
		date = [aDate retain];
	}
	return self;
	
}

Edit, NVM, look at this :

Code:
NSDate *d = [fmt dateFromString:[tempdict objectForKey:@"eve_date"]];
...
[eventPHP addObject:[Events eventsNamed:[tempdict objectForKey:@"title"] description:[tempdict objectForKey:@"description"] date:[tempdict objectForKey:@"eve_date"]]];

In your first line, why are you assuming [tempdict objectForKey😡"eve_date"] to return a NSString, but then you told me it returns an NSDate for the 2nd line ?

Something is not right there.
 
Last edited:
Edit, NVM, look at this :

Code:
NSDate *d = [fmt dateFromString:[tempdict objectForKey:@"eve_date"]];
...
[eventPHP addObject:[Events eventsNamed:[tempdict objectForKey:@"title"] description:[tempdict objectForKey:@"description"] date:[tempdict objectForKey:@"eve_date"]]];

In your first line, why are you assuming [tempdict objectForKey😡"eve_date"] to return a NSString, but then you told me it returns an NSDate for the 2nd line ?

Something is not right there.


ya i think in [ description ] because in my sql its text type but i xcode it is NSString. Is this wrong ?
 
ya i think in [ description ] because in my sql its text type but i xcode it is NSString. Is this wrong ?

No, it's ok, but you're sending an NSString as an argument to something that wants an NSDate. That's a bug.

As for your other selector exception, did you step through the code (using Step Into, not Step Over) to see what actual line triggers the exception ?
 
No, it's ok, but you're sending an NSString as an argument to something that wants an NSDate. That's a bug.

As for your other selector exception, did you step through the code (using Step Into, not Step Over) to see what actual line triggers the exception ?

i use NSLog :

Code:
- (id)initWithName:(NSString *)atitle description:(NSString *)adescription date:(NSDate *)aDate
{
	

if ((self = [super init])) {
		title = [atitle copy];
	NSLog(@"initWithName title = %@",title);
		description = [adescription copy];
	NSLog(@"initWithName description = %@",description);

		date = [aDate retain];
	NSLog(@"initWithName date = %@",date);

	//date = aDate;
	}
	return self;
	NSLog(@"I am here in initWithName");
	
}

all this property printed but

NSLog(@"I am here in initWithName");

dosen't printed 😕
 
What do you understand about the return statement and how it affects execution flow?
 
What do you understand about the return statement and how it affects execution flow?

because in my project there is many class one of them class [ Events] so
I'm not sure from "self = [super init]" 😕

i try self = [Events init];

but still exception exists
 
Code:
	self = [super initWithNibName:@"Events" bundle:nil];

	
	if (self != nil)
	{
		title = [atitle copy];
		NSLog(@"initWithName title = %@",title);
		description = [adescription copy];
		NSLog(@"initWithName description = %@",description);
		
		date = [aDate retain];
		NSLog(@"initWithName date = %@",date);
		
		
	}
	NSLog(@" self %@",self);
	NSLog(@"I am here in initWithName before return");

	return self;
	NSLog(@"I am here in initWithName");
	
}

when print this line this :

NSLog(@" self %@",self);


self *nil description*

why self print value of description 😕
 
Thank very much for all 😱

i solve my problem now just try solve

Code:
- (NSArray *)eventsFrom:(NSDate *)fromDate to:(NSDate *)toDate
{
	NSMutableArray *matches = [NSMutableArray array];
	for (Events *event in eventPHP)
	if (IsDateBetweenInclusive(event.date, fromDate, toDate))

			[matches addObject:event];
	NSLog(@"matches %@",matches);
	
	return matches;
}


static BOOL IsDateBetweenInclusive(NSDate *date, NSDate *begin, NSDate *end)
{
	return [date compare:begin] != NSOrderedAscending && [date compare:end] != NSOrderedDescending;
}

it is exception : bad execution



Thankxxxxxx for allll 😱
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.