0

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.

João Miranda
  • 487
  • 1
  • 5
  • 12
  • Set an [exception breakpoint](http://stackoverflow.com/a/17802723/74815) and have a look at the stack and see if that helps. It's not clear what you're trying to do. Can you give an example of your input and the expected output? – i_am_jorf Nov 19 '14 at 01:04
  • for this input:Object:[Event] FromJSON:[JSON](http://arenautfpr.com/DarkSide/SeuEvento/events_test.json), It should return an array of Event objects. – João Miranda Nov 19 '14 at 01:08
  • Using breakpoints, I found that the results array does not have any NSDictionary Objects, it has 3 NSNull * – João Miranda Nov 19 '14 at 01:12
  • Your sample JSON has an array as the root object. So `parsedObject` should be an `NSArray`, yes? – i_am_jorf Nov 19 '14 at 01:15
  • That makes sense, I've updated the code in the question, but it still gives me a 3 empty objects from the serialization. – João Miranda Nov 19 '14 at 01:28

1 Answers1

0
 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];
JNYJ
  • 515
  • 6
  • 14