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

Custom NSObject Class: No longer able to add objects

这是工作,然后它 stopped.I 撤消了更改,但仍然无法工作。而且,我有一个非常相似的自定义 NSObject class 可以正常工作。这是交易...

我有这个自定义 class,它最终会填充来自 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 class 中。但是,当 SKGreeting 对象 "greeting" 添加到 NSMutableArray 问候语时,它是空的。因此,最终结果是 "greetings" 内有一定数量的空对象。

+(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 对象添加任何内容。我不明白为什么代码没有将数据添加到 "greeting"。帮助。谢谢

试试这个方法[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];
        }
 }