-1

This was working and then it stopped.I've undone changes and it's still not working. And, I've got a very similar custom NSObject class that works fine. Here's the deal...

I have this custom class that will eventually be populated with data from a JSON object. Here's the .h file

#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

After I've received the JSON file, I run the following code to load it into the SKGreeting class. However, the SKGreeting object "greeting" is empty when it's added to the NSMutableArray greetings. So, the final result is some quantity of empty objects within "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;
}

As you can see, I've used several NSLog to make sure I'm getting content through the process, and I am. The setValue step doesn't add anything to the SKGreeting object. I can't figure out why the code isn't adding the data to "greeting". Help. Thanks.

SteveSTL
  • 998
  • 3
  • 13
  • 21
  • To be clear, if you set a break point on the `setValue:forKey:` method, it is being called? Update your question with the log output. – rmaddy Apr 11 '15 at 01:57
  • Yes, @rmaddy, it's being called. All 10 items from the JSON file are being logged to the output. I get output like: 2015-04-10 21:16:48.640 Project[3605:249142] rec key: 2014-09-03 12:14:58 2015-04-10 21:16:55.715 Revelate[3605:249142] sch1 key: 2014-09-03 00:00:01 2015-04-10 21:16:59.267 Project[3605:249142] img key: Farmers-market.jpg 2015-04-10 21:17:00.789 Project[3605:249142] sch2 key: 2015-09-10 00:00:01 2015-04-10 21:17:01.773 Project[3605:249142] sub key: Prague 2015-04-10 21:17:02.924 Project[3605:249142] typ key: 0 – SteveSTL Apr 11 '15 at 02:18
  • 1
    Hello anonymous down voter. Why? What did I do so wrong to earn your anonymous punishment? Thanks. – SteveSTL Apr 11 '15 at 13:42

1 Answers1

1

try this way [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];
        }
 }
thorb65
  • 2,696
  • 2
  • 27
  • 37