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

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

问题描述

我正在尝试为用户的Application Support文件夹获取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.

最佳做法是使用 NSSearchPathForDirectoriesInDomains with NSApplicationSupportDirectory 可能是long winded。

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'

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

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