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

ITCreative

macrumors member
Original poster
Aug 4, 2011
46
0
i read information in my database i use JSON but there is exception


'NSInvalidArgumentException', reason: '-[NSCFString JSONValue]: unrecognized selector sent to instance 0x6166970'

I think exception in this part

Code:
- (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) {

NSDate *d = [fmt dateFromString:[dict objectForKey:@"eve_date"]];
    NSLog(@"%@",d);
    [eventPHP addObject:[Events eventsNamed:[dict objectForKey:@"title_event"]  description:[dict objectForKey:@"description"] date:d]];



}
 
Which JSON library are you using?

in this http://code.google.com/p/json-framework/

----------

I try put NSLog to know which line is exception

when put NSlog before this line
Code:
	NSArray *array = [str JSONValue];

it is printed

after it doesn't printed :(

----------

Additional info :


I use kal source to represent event and read events from database server


{"event":[{"eve_date":"2011-12-18","eve_time":"00:00:06","title_event":"Google event","description":"Google event for IT student"},{"eve_date":"2011-12-29","eve_time":"00:00:08","title_event":"microsoft event","description":"microsoft event for IT student"}]}


and use JSON with kal source for calendar
 
So there is a category on NSObject. A are you including the .m as well as .h in your application? If not there is no compiled version of the category at runtime which would give exceptions like you are seeing.
 
I see the declaration in "NSObject+SBJson.h"

Make sure you have "SBJson.h" imported.

Also that all the library source files copied into your project.
 
ya i copy all file JSON

i try print str after

Code:
	NSString *str = [[[NSString alloc] initWithData:buffer encoding:NSUTF8StringEncoding] autorelease];
	NSLog(str);


print all contain in str

after that is crash !!!!
 
The runtime error indicates you may not have. Can you post a screenshot of your Project setup showing the .m files, specifically them getting compiled into your application executable?


in interface :

Code:
#import <Foundation/Foundation.h>
#import "Kal.h"

@class Events;
@interface EventsSqliteDataSource : NSObject <KalDataSource>

{
	NSMutableArray *items;
	NSMutableData *buffer;
	NSMutableArray *eventPHP;
	id<KalDataSourceCallbacks> callback;
	BOOL dataReady;
}
@property (nonatomic, retain) NSMutableArray *eventPHP;

+ (EventsSqliteDataSource *)dataSource;
- (Events *)EventsAtIndexPath:(NSIndexPath *)indexPath; 

@end




in implementation

Code:
#import "JSON.h"
#import "EventsSqliteDataSource.h"
#import "Events.h"

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

@interface EventsSqliteDataSource ()
- (NSArray *)eventsFrom:(NSDate *)fromDate to:(NSDate *)toDate;
- (NSArray *)markedDatesFrom:(NSDate *)fromDate to:(NSDate *)toDate;
@end


@implementation EventsSqliteDataSource
@synthesize eventPHP;


+ (EventsSqliteDataSource *)dataSource
{
	return [[[[self class] alloc] init] autorelease];
}

- (id)init
{
	if ((self = [super init])) {
		items = [[NSMutableArray alloc] init];
		eventPHP = [[NSMutableArray alloc] init];
		buffer = [[NSMutableData alloc] init];
	}
	return self;
}

- (Events *)EventsAtIndexPath:(NSIndexPath *)indexPath
{
	return [items objectAtIndex:indexPath.row];
}

#pragma mark UITableViewDataSource protocol conformance

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *identifier = @"MyCell";
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
	if (!cell) {
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
		cell.selectionStyle = UITableViewCellSelectionStyleNone;
		//cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
	}
	
	Events *event = [self EventsAtIndexPath:indexPath];
	//cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"flags/%@.gif", holiday.country]];
	
	//NSDictionary *dict = [eventPHP objectAtIndex: indexPath.row];
	
	//cell.textLabel.text = [dict objectForKey:@"title_event"];
	cell.textLabel.text = event.title;
	return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return [items count];
}

#pragma mark Fetch from the internet

- (void)fetchEvents
{
	NSString *path = @"http://www.iksu-app.com/event.php";
	NSLog(@"Fetching %@", path);
	dataReady = NO;
	[eventPHP removeAllObjects];
	NSURLConnection *conn = [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:path]] delegate:self];
	[conn start];
}

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

	NSArray *array = [str JSONValue];

	if (!array)
		return;
	
	NSLog(@"after this line crash");

	NSDateFormatter *fmt = [[[NSDateFormatter alloc] init] autorelease];
	[fmt setDateFormat:@"yyyy-MM-dd"];
	
	for (NSDictionary *dict in array) {
		NSDate *d = [fmt dateFromString:[dict objectForKey:@"eve_date"]];
	
		NSLog(@"%@",d);
		[eventPHP addObject:[Events eventsNamed:[dict objectForKey:@"title_event"] description:[dict objectForKey:@"description"] date:d]];
		
		
		
	}
	
	
	
	
	dataReady = YES;
	[callback loadedDataSource:self];
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
	NSLog(@"EventsCalendarDataSource connection failure: %@", error);
}

#pragma mark KalDataSource protocol conformance

- (void)presentingDatesFrom:(NSDate *)fromDate to:(NSDate *)toDate delegate:(id<KalDataSourceCallbacks>)delegate
{
	if (dataReady) {
		[callback loadedDataSource:self];
		return;
	}
	
	callback = delegate;
	[self fetchEvents];
}

- (NSArray *)markedDatesFrom:(NSDate *)fromDate to:(NSDate *)toDate
{
	if (!dataReady)
		return [NSArray array];
	
	return [[self eventsFrom:fromDate to:toDate] valueForKeyPath:@"eve_date"];
}

- (void)loadItemsFromDate:(NSDate *)fromDate toDate:(NSDate *)toDate
{
	if (!dataReady)
		return;
	
	[items addObjectsFromArray:[self eventsFrom:fromDate to:toDate]];
}

- (void)removeAllItems
{
	[items removeAllObjects];
}

#pragma mark -

- (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];
	
	return matches;
}

- (void)dealloc
{
	[items release];
	[eventPHP release];
	[buffer release];
	[super dealloc];
}



@end


161558_q94.png



:(
 
None of that is what I have asked for: are the .m files that form the actual implementation of the various headers included by JSON.h actually in your project. You are seeing an error that indicates they are not. You are including the headers so the NSObject category is available at compile time. But the error is very clear: it's not available at run time. So the most likely outcome is you have not included the .m files in the project. Once again: please provide a screenshot showing that you have included the .m files in your project and that they are being compiled into the final executable.
 
Is should my file which read from data server "event.json" ?

or not problem with "event.php" ?

because the example holiday which trace it the end of url ".json"
 
I solve exception by change location files class of JSON but the event doesn't

display in calendar .

the calendr is display but didn't contain any event :confused:

hint: when i put url in browser the content of database display
 
yes, i have faced this problem too. This code will work in score 4.2 , 3.2 etc But the latest Xcode Give you the NSString Error on the Line where you used JSONValue. Idont know why this error occurs in Xcode 4.3.2. But this code will run on Xcode 4.2. check it using Xcode 4.2 or 3.2
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.