以编程方式获取应用程序支持文件夹的路径 [英] Programmatically get path to Application Support folder

查看:25
本文介绍了以编程方式获取应用程序支持文件夹的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为用户的应用程序支持文件夹获取 NSString.

I'm trying to get an NSString for the user's Application Support folder.

我知道我可以做到 NSString *path = @"~/Library/Application Support"; 但这似乎不太优雅.我已经尝试过使用 NSSearchPathForDirectoriesInDomains 但它似乎很冗长并且创建了几个不必要的对象(至少,我的实现是这样的).

I know I can do NSString *path = @"~/Library/Application Support"; but this doesn't seem very elegant. I've played around with using NSSearchPathForDirectoriesInDomains but it seems to be quite long-winded and creates several unnecessary objects (at least, my implementation of it does).

有没有简单的方法可以做到这一点?

Is there a simple way to do this?

推荐答案

这已经过时了,对于当前的最佳实践,请使用 FileManager.default.urls(for:in:) 作为评论中的 @andyvn22 下面.

This is outdated, for current best practice use FileManager.default.urls(for:in:) as in the comment by @andyvn22 below.

最佳实践是使用 NSSearchPathForDirectoriesInDomainsNSApplicationSupportDirectory 尽可能冗长".

the Best practice is to use NSSearchPathForDirectoriesInDomains with NSApplicationSupportDirectory as "long winded" as it may be.

示例:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *applicationSupportDirectory = [paths firstObject];
NSLog(@"applicationSupportDirectory: '%@'", applicationSupportDirectory);

NSLog 输出:

applicationSupportDirectory: '/Volumes/User/me/Library/Application Support'

这篇关于以编程方式获取应用程序支持文件夹的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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