[英]How do I add files to /Documents directory in App Sandbox
這是我的位置:
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.
盡管在本網站和其他網站上都搜索了完成上述操作的一組說明,但我還是很簡短。
我也查看了Apple的文檔,發現了切向信息,但沒有直接了解如何完成上述操作。
這是我的代碼:
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];
概括一下:如何在App Sandbox的/ Documents目錄中添加文件?
您要到達的文檔目錄與您想要的目錄不同。 您需要捆綁包中的“文檔”目錄。
[[[NSBundle mainBundle] pathsForResourcesOfType:@"json" inDirectory:nil] enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL *stop) {
^這將從您的捆綁軟件(您在項目中拖動的文件)中獲取所有json類型的文件,而無需指定文件夾。 通過在所需捆綁包中指定文件夾,可以從所需目錄中獲取特定的文件夾。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.