0

I have simple Cocoa application, that parse JSON and pack information of JSON to object of Day class.

Day.h.

#import <Foundation/Foundation.h>

@interface Day : NSObject

@property(retain, nonatomic) NSString *dateString;

@end

main.m

import <Foundation/Foundation.h>
import </Users/Admin/Documents/SimpleJsonParser/SBJSON.h>
#import "Day.h"

int main(int argc, const char * argv[])
{

    @autoreleasepool {

        // source json object
        NSString *jsonSource = @"{\"days\":[{\"dateString\":\"10 december\"},{\"dateString\":\"11 december\"}]}";

        SBJsonParser *jsonParser = [[SBJsonParser alloc] init];

        NSError *error = nil;

        NSDictionary *jsonObjects = [jsonParser objectWithString:jsonSource error:&error];

        if(error != nil){
            NSLog([error description]);
        }

        // array of days objects
        NSArray *days = [jsonObjects objectForKey:@"days"];

        // create empty array
        NSMutableArray *daysSource = [[NSMutableArray array] retain];

        for(int i = 0; i < [days count]; i++){

            NSDictionary *day = [days objectAtIndex:i];

            // get dateString
            NSString *dateString = [day objectForKey:@"dateString"];

            // create Day object
            Day* dayObject = [[Day alloc] init];

            dayObject.dateString = dateString;

            NSLog(dayObject.dateString);            

            [daysSource addObject:day];
        }

        NSUInteger temp = [daysSource count];
        NSLog(@"Temp is %lu", temp);

        Day *myDay = [daysSource objectAtIndex:0];

        NSString *dateStringTemp = myDay.dateString;
    }
}

And when I run this code, I get this error:

2012-12-11 14:09:50.867 SimpleJsonParser[577:303] -[__NSDictionaryM dateString]: unrecognized selector sent to instance 0x10010e4d0
2012-12-11 14:09:50.869 SimpleJsonParser[577:303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryM dateString]: unrecognized selector sent to instance 0x10010e4d0'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff90a2e0a6 __exceptionPreprocess + 198
    1   libobjc.A.dylib                     0x00007fff8c6713f0 objc_exception_throw + 43
    2   CoreFoundation                      0x00007fff90ac46ea -[NSObject(NSObject) doesNotRecognizeSelector:] + 186
    3   CoreFoundation                      0x00007fff90a1c5ce ___forwarding___ + 414
    4   CoreFoundation                      0x00007fff90a1c3b8 _CF_forwarding_prep_0 + 232
    5   SimpleJsonParser                    0x0000000100001700 main + 704
    6   libdyld.dylib                       0x00007fff850b57e1 start + 0
    7   ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminate called throwing an exception
(lldb) 

Why property "dateString" of Day class saved as NSDictionary?

1
  • You seem to be confusing yourself about the types of object you are working with. Adopt a naming convention which tells you what type it is, for example jsonObjectDict, daysArray, daysSourceArray and dayDict. Commented Dec 11, 2012 at 11:58

3 Answers 3

1

You are adding the dictionary object to array not Day object.

Change:

[daysSource addObject:day];

to

[daysSource addObject:dayObject];
Sign up to request clarification or add additional context in comments.

Comments

1

The object returned from [daysSource objectAtIndex:0] is an NSDictionary

NSDictionary *myDay = [daysSource objectAtIndex:0];

NSString *dateStringTemp = [myDay objectForKey:@"dateString"];

1 Comment

or new style syntax : NSString *dateStringTemp = myDay[@"dateString"];
0

day is a dictionary. You store it in daySource and then do Day *myDay = [daysSource objectAtIndex:0];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.