我自己的目录的位置

发布于 2024-08-22 10:40:18 字数 423 浏览 16 评论 0原文

我试图了解如何创建自己的目录树并从我的应用程序访问它。

我正在做某种学习计划,想要创建一个像这样的目录结构:

-MyLessonsDirectory
--LessonDirectory_1
---Step_1
---Step_2
---Step_3
--LessonDirectory_2
---Step_1
---Step_2
---Step_3
.
.
and so on

在每个“步骤”目录中,我都会有该课程步骤的特定内容。内容是我在开发时添加的。因此,我不想从应用程序内创建结构和内容,只需访问它并读取目录的内容。如果我可以创建这种结构并使用它,那么创建一个漂亮的设计将非常容易,从而可以轻松地在我的应用程序中创建新课程。

我不知道应该在 xCode 的文件结构中的哪里创建这些目录以及从我的应用程序到这些目录的路径是什么。

I am trying to understand how to create my own directory tree and access it from my application.

I am doing some kind of learning program and want to create a directory structure like this:

-MyLessonsDirectory
--LessonDirectory_1
---Step_1
---Step_2
---Step_3
--LessonDirectory_2
---Step_1
---Step_2
---Step_3
.
.
and so on

In each "Step"-directory I will have specific content for that step of the lesson. The content is added by me while developing. Hence, I don't want to create the structure and content from within the application, just access it and reading the content of the directories. If I can create this kind of structure and use it it will be very easy to create a nice design that makes it easy to create new lessons in my app.

What I cannot figure out is where I should create those directories in the file-structure in xCode and what is the path to those directories from my application.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

筱武穆 2024-08-29 10:40:18

你想看看 NSFileManager 来创建目录:

NSFileManager *fm = [NSFileManager defaultManager];
NSError *error;
BOOL result = [fm createDirectoryAtPath:@"path....." withIntermediateDirectories:YES attributes:nil error:&error];
if(result){
   NSLog(@"success!");
} else {
   NSLog(@"error: %@",error);
}

至于在哪里创建目录,你可以把它放在 ~/Application Support/--Your App's Name-- 如果用户不这样做的话,这就是你应该存储应用程序数据的地方不需要访问它。否则,您应该让用户决定将其保存在哪里。

- (NSString *)applicationSupportDirectory {

       NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
       NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : NSTemporaryDirectory();
       return [basePath stringByAppendingPathComponent:@"MyApplication"];
}

You want to look at NSFileManager to create directories:

NSFileManager *fm = [NSFileManager defaultManager];
NSError *error;
BOOL result = [fm createDirectoryAtPath:@"path....." withIntermediateDirectories:YES attributes:nil error:&error];
if(result){
   NSLog(@"success!");
} else {
   NSLog(@"error: %@",error);
}

As far as where to create the directories, you can put it in ~/Application Support/--Your App's Name-- This is where you should store your app's data if the user doesn't require access to it. Otherwise, you should let the user decide where to save it.

- (NSString *)applicationSupportDirectory {

       NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
       NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : NSTemporaryDirectory();
       return [basePath stringByAppendingPathComponent:@"MyApplication"];
}
左岸枫 2024-08-29 10:40:18

在 iPhone 上,您只能访问应用程序自己的目录。练习目录最好放在应用程序目录的 Documents 文件夹中。

这...

NSString *dp=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

...为您提供了目录文件夹的路径,然后您可以将其交给 NSFileManager。

如果您想直观地查看目录结构,请在模拟器上运行该应用程序并查看

~/Library/Application Support/iPhone Simulator/User/Applications

该应用程序将位于具有乱码 UUID 名称的文件夹之一中。您可以在 Finder 中查看目录和文件。

我想补充一点,iPhone 已经拥有您可能实际需要的大部分目录。您已准备好首选项、缓存、tmp、文档等。

编辑01:

我想做的是添加内容
在我开始之前手动
应用。然后,从内部
应用程序我将阅读内容我
在启动应用程序之前添加。

您无法更改默认目录结构,并且完全无法控制应用程序或其目录的安装方式。 iPhone 应用程序没有任何类型的外部支持文件。所有内容都在应用程序包内。

然而,应用程序包实际上只是一个目录,因此您可以在其中填充内容,并在应用程序第一次运行时将它们复制到默认目录之一。您可以在文档目录中写入任何您想要的内容。

On the iPhone, you only have access to the apps own directories. The best place to put practice directories in the Documents folder in the app directory.

This...

NSString *dp=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

... gives you the path to the directory folder which you can then hand off to the NSFileManager.

If you want to see the directory structure visually, run the app on the simulator and look in

~/Library/Application Support/iPhone Simulator/User/Applications

The app will be in one of the folders with the gibberish UUID name. You can see the directories and files there in the Finder.

I would add that the iPhone already has most of the directories you might practically need. You have preferences, cache, tmp, Documents etc all in place.

Edit01:

What I want to do is to add content
manually before I start the
application. Then, from within the
application I will read the content I
added before starting the application.

You cannot alter the default directory structure and you have absolutely no control over how the application or its directories are installed. iPhone apps do not have external support files of any kind. Every thing is inside the app bundle.

However, the app bundle is really just a directory so you can stuff things inside it and copy them out into one of the default directories the first time the app runs. You can write anything you want to the documents directory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文