Creating Objects from template crashes with error: "NSInvalidArgumentException"

141 views Asked by At

This function is intended to get a JSON and make an array of objects based on the object sent as parameter:

+ (NSArray *)Object: (id) object FromJSON:(NSData *)objectNotation error:(NSError  **)error
{
    NSError *localError = nil;
    NSArray *parsedObject = [NSJSONSerialization JSONObjectWithData:objectNotation options:0 error:&localError];

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

    NSMutableArray *output = [[NSMutableArray alloc] init];

    NSArray *results = [parsedObject valueForKey:@"results"];
    NSLog(@"Count %lu", (unsigned long)results.count);

    for (NSArray *eventDic in results) {
        object = [[[object class] alloc] init];

        for (NSString *key in eventDic) {
            if ([object respondsToSelector:NSSelectorFromString(key)]) {
                [object setValue:[eventDic valueForKey:key] forKey:key];
            }
        }

        [output addObject:object];
    }

    return output;
}

But it is crashing every time I run it. I get this error:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x1100c3ce0'

I am new to iOS programming and have no idea what this means.

1

There are 1 answers

0
JNYJ On BEST ANSWER
 NSArray *parsedObject = [NSJSONSerialization JSONObjectWithData:objectNotation options:0 error:&localError];

Pass 0 if you don't care about the readability of the generated string

if you want to parsed object as NSArray, change your options as NSJSONReadingMutableContainers

NSDictionary *dic_JSON = [NSJSONSerialization JSONObjectWithData: jsonData
                                options: NSJSONReadingMutableContainers
                                  error: &error];