自定义NSObject类:不再能够添加对象

时间:2015-04-11 01:47:12

标签: ios objective-c nsobject

这是有效的,然后停止了。我已经撤消了更改,但它仍然无法正常工作。而且,我有一个非常类似的自定义NSObject类,工作正常。这是交易......

我有这个自定义类,最终将使用来自JSON对象的数据填充。这是.h文件

#import <Foundation/Foundation.h>

@interface SKGreeting : NSObject <NSCoding>

@property (strong, nonatomic) NSNumber *gid;
@property (strong, nonatomic) NSString *sub;
@property (strong, nonatomic) NSString *mes;
@property (strong, nonatomic) NSString *usr;
@property (strong, nonatomic) NSDate *rec;
@property (strong, nonatomic) NSDate *sch1;
@property (strong, nonatomic) NSDate *sch2;
@property (strong, nonatomic) NSString *img; // name of the image
@property (strong, nonatomic) NSString *itp; // image type
@property (strong, nonatomic) NSNumber *typ;

@property (strong, nonatomic) UIImage *image; //actual image

@end

收到JSON文件后,我运行以下代码将其加载到SKGreeting类中。但是,SKGreeting对象“greeting”在添加到NSMutableArray问候语时为空。因此,最终结果是“问候”中的一些空对象。

+(NSMutableArray *)greetingsFromJSON:(NSData *)objectNotation error:(NSError **)error
{
    NSError *localError = nil;
    NSDictionary *parsedObject = [NSJSONSerialization JSONObjectWithData:objectNotation options:0 error:&localError];

    if(localError != nil) {
        *error = localError;
        return nil;
    }

    NSMutableArray *greetings = [[NSMutableArray alloc] init];
//    NSLog(@"Greetings: %@", greetings);
//    NSLog(@"Parsed Object: %@", parsedObject);

    NSArray *results = [parsedObject valueForKey:@"results"];
//    NSLog(@"Results Record Count: %d", results.count);
//    NSLog(@"array results: %@", results);

    for (NSDictionary *greetDic in results)
    {
        SKGreeting *greeting = [[SKGreeting alloc] init];

        for (NSString *key in greetDic)
        {
            if([greeting respondsToSelector:NSSelectorFromString(key)])
            {
                NSLog(@"%@ key: %@",key,[greetDic valueForKey:key]);
                [greeting setValue:[greetDic valueForKey:key] forKey:key];
            }
        }

        [greetings addObject:greeting];
    }

    // Save the data received to the user defaults
    NSData *encodedGreetings = [NSKeyedArchiver archivedDataWithRootObject:greetings];
    [[NSUserDefaults standardUserDefaults] setObject:encodedGreetings forKey:@"greetingsEncoded"];

    return greetings;
}

正如你所看到的,我已经使用了几个NSLog来确保我在整个过程中获得内容,而我是。 setValue步骤不会向SKGreeting对象添加任何内容。我无法弄清楚为什么代码没有将数据添加到“问候”。救命。感谢。

1 个答案:

答案 0 :(得分:1)

尝试[greetDic allKeys]

 for (NSString *key in [greetDic allKeys]) {
        if([greeting respondsToSelector:NSSelectorFromString(key)])
        {
            NSLog(@"%@ key: %@",key,[greetDic valueForKey:key]);
            [greeting setValue:[greetDic valueForKey:key] forKey:key];
        }
 }