如何将文件添加到 App Sandbox 中的/Documents 目录 [英] How do I add files to /Documents directory in App Sandbox

查看:22
本文介绍了如何将文件添加到 App Sandbox 中的/Documents 目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里:

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.

我还查看了 Apple 的文档,并找到了相关信息,但没有直接描述如何完成上述操作.

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

这是我的代码:

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 目录?

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

推荐答案

您在那里获得的文档目录与您想要的目录不同.您需要捆绑包中的文档"目录.

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) {

^ 这将从您的包中获取所有 json 类型的文件(您在项目中拖动的文件),您不必指定文件夹.您可以通过指定您想要的包中的文件夹,从您想要的目录中获取特定的.

^ 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.

这篇关于如何将文件添加到 App Sandbox 中的/Documents 目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆