how solve NSCFString objectForKey:

Discussion in 'iOS Programming' started by ITCreative, Dec 21, 2011.

  1. ITCreative macrumors member

    Joined:
    Aug 4, 2011
    #1
    in my code there is exception:

    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

    plz help me to solve it i search to find way to solve it but didn't found result. :(
     
  2. KnightWRX macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #2
    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:.
     
  3. ITCreative thread starter macrumors member

    Joined:
    Aug 4, 2011
    #3

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


    str :

    then print array :

    the exception happen


     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    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.
     
  5. ITCreative thread starter macrumors member

    Joined:
    Aug 4, 2011
    #5


    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





    ok thanx :eek:, i try solve it.
     
  6. KnightWRX macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #6
    What is [dict valueForKeyPath:mad:"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.
     
  7. ITCreative thread starter macrumors member

    Joined:
    Aug 4, 2011
    #7
    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

    how solve this exception ?
     
  8. KnightWRX macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #8
    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.
     
  9. ITCreative thread starter macrumors member

    Joined:
    Aug 4, 2011
    #9

    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 :confused:
     
  10. KnightWRX macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #10
    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'
     
  11. ITCreative thread starter macrumors member

    Joined:
    Aug 4, 2011
    #11
    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;
    	
    }
    
    
     
  12. KnightWRX, Dec 25, 2011
    Last edited: Dec 25, 2011

    KnightWRX macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #12
    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:mad:"eve_date"] to return a NSString, but then you told me it returns an NSDate for the 2nd line ?

    Something is not right there.
     
  13. ITCreative thread starter macrumors member

    Joined:
    Aug 4, 2011
    #13

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

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #14
    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 ?
     
  15. ITCreative thread starter macrumors member

    Joined:
    Aug 4, 2011
    #15
    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 :confused:
     
  16. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #16
    What do you understand about the return statement and how it affects execution flow?
     
  17. ITCreative thread starter macrumors member

    Joined:
    Aug 4, 2011
    #17
    because in my project there is many class one of them class [ Events] so
    I'm not sure from "self = [super init]" :confused:

    i try self = [Events init];

    but still exception exists
     
  18. ITCreative thread starter macrumors member

    Joined:
    Aug 4, 2011
    #18
    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);


    why self print value of description :confused:
     
  19. ITCreative thread starter macrumors member

    Joined:
    Aug 4, 2011
    #19
    Thank very much for all :eek:

    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 :eek:
     
  20. ITCreative thread starter macrumors member

    Joined:
    Aug 4, 2011
    #20
    Thanxxxxxxxxxxxxxx very much for all :eek:

    Now my code fine
     

Share This Page