2

Here is where I am:

I have JSON files I need to mount into Objective C Objects

I have an XCode Project

I have dragged the folder of JSON files into my project and 
created a blue "Documents" folder off of the root of the project.

However, it does not appear in the app sandbox in the ~/Library/... folder. 

Despite searching both this site and others for a set of instructions to accomplish the above I have come up short.

I have also reviewed Apple's docs, and have found tangential information but not a straight forward description of how to accomplish the above.

Here is my code:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *lessonsDirectory = [documentsDirectory stringByAppendingPathComponent:@"/lessons"];

NSString *filePath = [lessonsDirectory stringByAppendingPathComponent:@"/52aa2b6e9af9b73896095404.json"];
NSLog( filePath );

NSData *data = [NSData dataWithContentsOfFile:filePath];

//THE NEXT LINE ERRORS OUT -> "data parameter is nil'
NSArray *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

To recapitulate: How do I add files to /Documents directory in App Sandbox?

Guido Anselmi
  • 3,872
  • 8
  • 35
  • 58
  • The code you posted is for reading a file from `Documents`. Use similar code to write data to a file in `Documents`. – rmaddy Jun 09 '14 at 21:17
  • If you want to pre-load Documents with files you must have some sort of first-time-through logic that copies the files from the bundle. If you dig around a bit this is a common question asked when "priming" a SQLite database on iOS -- same basic problem, and easily a dozen code samples to look at. – Hot Licks Jun 09 '14 at 21:42
  • Take a look at the NSData class reference in Xcode. Search for methods with the word "write" in the method name. I'm not going to spoon-feed you the answer though. Go look at the docs and see if you can figure it out. – Duncan C Jun 10 '14 at 00:15

1 Answers1

0

The documents directory that you are getting there is different form the one you want. You need the "Documents" directory from the bundle.

[[[NSBundle mainBundle] pathsForResourcesOfType:@"json" inDirectory:nil] enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL *stop) {

^ this will get all files of type json from ur bundle (the ones you dragged in your project) you dont have to specify folder. You can get specific ones from the directory you want by specifying the folder in the bundle you want.

adrian.coroian
  • 582
  • 4
  • 13
  • The app bundle is not a different documents directory. It is a different directory, but is not a documents directory. If the OP only ever needs to read the files, and not write them, then yes, he could read them from the app bundle, but don't mix up terms like that. You'll just confuse things. – Duncan C Jun 10 '14 at 00:11
  • 1
    From the code posted the OP seems to want to read files from somewhere. He doesnt seem to want to write stuff anywhere. He adds some files to his project and wants to get the data from them. I posted code related to that. The only reason he seems to say he wants to add files to sandbox is because his current way of doing it seems not to work for him. That is what i have come up with from the description he gave. I never said you could write files to bundle but im glad you clarified that point. – adrian.coroian Jun 10 '14 at 00:25
  • Thanks. I actually need to store the files in separate directories as they are of different types and I need to iterate through them at app launch. – Guido Anselmi Jun 10 '14 at 15:02