Cocoa相当于.NET的Environment.SpecialFolder保存首选项/设置? [英] Cocoa equivalent of .NET's Environment.SpecialFolder for saving preferences/settings?

查看:173
本文介绍了Cocoa相当于.NET的Environment.SpecialFolder保存首选项/设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Xcode中编写Objective-C Cocoa应用程序时,如何获取用于存储每个应用程序设置的文件夹的引用?

How do I get the reference to a folder for storing per-user-per-application settings when writing an Objective-C Cocoa app in Xcode?

NET我将使用 Environment.SpecialFolder 枚举:

In .NET I would use the Environment.SpecialFolder enumeration:

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

Cocoa等价物是什么?

What's the Cocoa equivalent?

推荐答案

在Mac OSX应用程序首选项是通过NSUserDefaults自动存储,将它们保存到.plist文件〜/ Library / Preferences / 。您不应该对此文件执行任何操作,NSUserDefaults将为您处理一切。

In Mac OSX application preferences are stored automatically through NSUserDefaults, which saves them to a .plist file ~/Library/Preferences/. You shouldn't need to do anything with this file, NSUserDefaults will handle everything for you.

如果您在非基于文档的应用程序中有数据文件AddressBook.app),您应该将其存储在〜/ Library / Application Support / Your App Name / 中。没有内置的方法来找到或创建这个文件夹,你需要自己去做。这里有一个我自己的应用程序的例子,如果你看一些Xcode项目模板,你会看到一个类似的方法。

If you have a data file in a non-document based application (such as AddressBook.app), you should store it in ~/Library/Application Support/Your App Name/. There's no built-in method to find or create this folder, you'll need to do it yourself. Here's an example from one of my own applications, if you look at some of the Xcode project templates, you'll see a similar method.

+ (NSString *)applicationSupportFolder;
{
    // Find this application's Application Support Folder, creating it if 
    // needed.

    NSString *appName, *supportPath = nil;
    NSArray *paths = NSSearchPathForDirectoriesInDomains( NSApplicationSupportDirectory, NSUserDomainMask, YES );

    if ( [paths count] > 0)
    {
        appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleExecutable"];
        supportPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:appName];

        if ( ![[NSFileManager defaultManager] fileExistsAtPath:supportPath] )
            if ( ![[NSFileManager defaultManager] createDirectoryAtPath:supportPath attributes:nil] )
                supportPath = nil;
    }

    return supportPath;
}

请记住,如果您的应用程序很受欢迎,能够为共享同一帐户的不同用户提供多个库文件。如果你想支持这个,约定是提示一个路径,当应用程序启动时按住alt / option键。

Keep in mind that if your app is popular you'll probably get requests to be able to have multiple library files for different users sharing the same account. If you want to support this, the convention is to prompt for a path to use when the application is started holding down the alt/option key.

这篇关于Cocoa相当于.NET的Environment.SpecialFolder保存首选项/设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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