这是我所在的地方:
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.
尽管在这个网站和其他网站上搜索了一组完成上述操作的说明,但我还是没有找到。
我还查看了苹果的文档,发现了一些无关紧要的信息,但没有直接描述如何完成上述任务。
这是我的代码:
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];
概括一下:如何将文件添加到应用程序沙箱中的 /Documents 目录?
您获得的文档目录与您想要的不同。您需要捆绑包中的“Documents”目录。
[[[NSBundle mainBundle] pathsForResourcesOfType:@"json" inDirectory:nil] enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL *stop) {
^ 这将从您的捆绑包中获取 json 类型的所有文件(您在项目中拖动的文件),您不必指定文件夹。您可以通过指定所需捆绑包中的文件夹来从所需目录中获取特定的文件。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)