对象未保存在数组中

时间:2015-03-24 08:29:18

标签: ios objective-c iphone

我有以下代码将json解析为自定义对象,解析工作正常我有保存对象的问题。

-(void)handleCities:(NSNotification *)notification
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"_getInitialData" object:nil];

if(![notification object])
{
    NSLog(@"Something went wrong");

    return;
}

// Convert data to a dictionary
NSMutableDictionary * data = [NSJSONSerialization JSONObjectWithData:(NSMutableData *) [notification object] options:NSJSONReadingMutableContainers error:nil];

// Loop over all the data
if([data objectForKey:@"data"] != (id)kCFBooleanFalse)
{
    for(int i = 0; i < [[[data objectForKey:@"data"] objectForKey:@"cities"] count]; i++)
    {
        CityObject * object = [[CityObject alloc] init];

        // Loop over all the properties
        for(NSString * key in [[[data objectForKey:@"data"] objectForKey:@"cities" ] objectAtIndex:i])
        {
            NSString * _key = key;

            if([object._remap objectForKey:key])
            {
                NSString * value = [object._remap objectForKey:_key];

                NSLog(@"Notice [%@] Remapping \"%@\" to \"%@\"", self.class, _key, value);

                _key = value;
            }

            // Save the value for easy access
            NSString * value = [[[[data objectForKey:@"data"] objectForKey:@"cities" ] objectAtIndex:i] objectForKey:_key];

            // Does the value have a value
            if((id)value == [NSNull null])
            {
                value = @"";
            }

            // Set the value for key
            [object setValue:value forKey:_key];
        }

        NSMutableDictionary * set = [[[data objectForKey:@"data"] objectForKey:@"cities"] objectAtIndex:i];

        int count = (int)[[set objectForKey:@"categories"] count];

        for(int b = 0; b < count; b++)
        {
            CategoryObject * category = [[CategoryObject alloc] init];

            for(NSString * key in [[set objectForKey:@"categories"] objectAtIndex:b])
            {
                NSString * value = [[[set objectForKey:@"categories"] objectAtIndex:b] objectForKey:key];

                if((id)value == [NSNull null])
                {
                    value = @"";
                }

                [category setValue:value forKey:key];
            }

            NSLog(@"Category %@",category.class);
            NSLog(@"Object : %@",object.class);
            NSLog(@"Object Categories : %@",object.categories.class);

            // i can print the category object fine here

            [object.categories addObject:(CategoryObject *)category];
        }

        [self.cities addObject:object];
    }
}

CityObject * city = [self.cities objectAtIndex:0];
CategoryObject * category = [city.categories objectAtIndex:0];
NSLog(@"Category Name : %@", category.name);

[[NSNotificationCenter defaultCenter] removeObserver:self name:@"_getInitialData" object:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"getInitialData" object:nil];
}

CityObject.h

#import <Foundation/Foundation.h>
#import "Object.h"

@interface CityObject : Object

@property (nonatomic, strong) NSString * name;
@property (nonatomic, strong) NSString * id;
@property (nonatomic, strong) NSString * state;
@property (nonatomic, strong) NSString * image;
@property (nonatomic, strong) NSString * term_order;

@property (nonatomic, strong) NSMutableArray * categories;

@property (nonatomic, strong) NSMutableDictionary * _remap;


@end

CityObject.m

#import "CityObject.h"

@implementation CityObject

-(id)init
{
if(self = [super init])
{
    self._remap = [[NSMutableDictionary alloc] init];
    self.categories = [[NSMutableArray alloc] init];

    [self._remap setObject:@"state" forKey:@"new_city"];
}

return self;
}

@end

CategoryObject.h

#import <Foundation/Foundation.h>
#import "Object.h"

@interface CategoryObject : Object

@property (nonatomic, strong) NSString * name;
@property (nonatomic, strong) NSString * icon;
@property (nonatomic, strong) NSString * id;

@end

在我将CategoryObject添加到我的数组之前,我可以检索到没有问题的值,当我在最后的NSLog时,我无法从数组中获取值/对象。

我做错了什么,因为当我添加CityObjects时效果很好,但是当我“解析”类别时却没有。我可以在将它们添加到object.categories

之前阅读它们

错误/输出

-[__NSDictionaryM name]: unrecognized selector sent to instance 0x7a482760
2015-03-24 09:21:05.649 10things[16968:337325] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryM name]: unrecognized selector sent to instance 0x7a482760'

1 个答案:

答案 0 :(得分:0)

问题解决了,我没有一个强大的指针模型,字典神奇地被替换为NSMutableDictionary一旦我有一个强大的处理一切正常。 (感谢代码反馈)