从数组元素属性获取对象时出错

时间:2012-12-11 11:42:07

标签: objective-c ios

我有简单的Cocoa应用程序,它解析JSON并将JSON的信息打包到Day类的对象。

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;
    }
}

当我运行此代码时,我收到此错误:

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) 

为什么Day类的属性“dateString”保存为NSDictionary?

3 个答案:

答案 0 :(得分:1)

[daysSource objectAtIndex:0]返回的对象是NSDictionary

NSDictionary *myDay = [daysSource objectAtIndex:0];

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

答案 1 :(得分:1)

您正在将dictionary对象添加到数组而不是Day对象。

变化:

[daysSource addObject:day];

[daysSource addObject:dayObject];

答案 2 :(得分:0)

天是一本字典。您将其存储在daySource中,然后执行Day *myDay = [daysSource objectAtIndex:0];