syedfa
syedfa

Reputation: 2809

Trying to read JSON file in Objective-C

I am trying to read in a json file in my Objective-C application, but unfortunately I am getting a RuntimeException. The exact error is:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'

My json file is called "Transactions.json

[{ "transaction": "300001", "date": "1/1/11", "type": "ABC", "status": "State1" },
  { "transaction": "300002", "date": "2/2/12", "type": "XYZ", "status": "State2" },
  { "transaction": "300003", "date": "3/3/13", "type": "ABC", "status": "State3" },
  { "transaction": "300004", "date": "2/2/12", "type": "XYZ", "status": "State2" },
  { "transaction": "300005", "date": "3/3/13", "type": "ABC", "status": "State3" },
  { "transaction": "300006", "date": "2/2/12", "type": "XYZ", "status": "State2" },
  { "transaction": "300007", "date": "3/3/13", "type": "ABC", "status": "State3" },
  { "transaction": "300008", "date": "2/2/12", "type": "XYZ", "status": "State2" },
  { "transaction": "300009", "date": "3/3/13", "type": "ABC", "status": "State3" },
  { "transaction": "300010", "date": "4/4/14", "type": "XYZ", "status": "State4" } ]

My method that reads in the file looks like this:

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        // Create the managed object context
        NSManagedObjectContext *context = managedObjectContext();
        
        // Custom code here...
        // Save the managed object context
        NSError *error = nil;
        if (![context save:&error]) {
            NSLog(@"Error while saving %@", ([error localizedDescription] != nil) ? [error localizedDescription] : @"Unknown Error");
            exit(1);
        }
        
        NSError *err = nil;
        NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"Transactions" ofType:@"json"];
        NSLog(@"Hello: %@", dataPath);
        NSArray *transaction = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:dataPath] options:kNilOptions error:&err];
        
                NSLog(@"Transaction list: %@", transaction);
        
    }
    
    return 0;
}

What I don't understand is why is the NSData object coming up as null, when I am reading in the JSON file? What am I doing wrong? Just for the record, I have tried checking for extraneous spaces in my JSON file, and tried placing the JSON file in different places within my application folder, and nothing has worked.

Upvotes: 2

Views: 4202

Answers (1)

Martin R
Martin R

Reputation: 539815

From your source code and your comments I assume that the application is built as a "Command Line Tool". In that case the "Target MemberShip" checkbox is actually disabled for custom resource files like your JSON file.

As a workaround:

  • Go to the "Build Phases" of the target.
  • Click on "Add Build Phase" and select "Add Copy Files".
  • In the new build phase, click on + and select your JSON file.

For a command line tool, it does not seem to matter which "Destination" you choose. I tested it with "Destination: Resources". The JSON file is copied in the same directory that contains the executable file. Now

[[NSBundle mainBundle] pathForResource:@"Transactions" ofType:@"json"];

should return that path and you can read the file from your application.

Upvotes: 2

Related Questions