user1083392
user1083392

Reputation: 23

Error when get object from array element property

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?

Upvotes: 0

Views: 1462

Answers (3)

Midhun MP
Midhun MP

Reputation: 107191

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

Change:

[daysSource addObject:day];

to

[daysSource addObject:dayObject];

Upvotes: 1

Hjalmar
Hjalmar

Reputation: 989

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

Upvotes: 0

Vinny Coyne
Vinny Coyne

Reputation: 2365

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

NSDictionary *myDay = [daysSource objectAtIndex:0];

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

Upvotes: 1

Related Questions