在 mac os 上使用 cocoa 获取 private/var/folders 缓存文件夹路径

Get private/var/folders cache folder path with cocoa on mac os

我正在开发一个应用程序,我需要在 macOs 缓存文件夹中存储一些文件。由于每个用户的路径都会改变,我无法确定它。例如我的是:

/var/folders/_9/q7r9pg8s31g5m96jf7q3h3j80000gn/C/

我知道

这样的方法
NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSAllDomainsMask, YES);

但它只给我:

"/Users/me/Library/Caches" -- "/Library/Caches" -- "/System/Library/Caches"

这不是我需要的。我正在寻找与 NSTemporaryDirectory() 等效的缓存文件夹。

我发现获取它的唯一方法是使用终端并键入 getconf DARWIN_USER_CACHE_DIR ...

您必须通过添加目录来扩展 Caches 文件夹,如下所示:

NSString *path = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(
NSCachesDirectory, NSUserDomainMask, YES);
if ([paths count])
{
    NSString *bundleName =
    [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"];
    path = [[paths objectAtIndex:0] stringByAppendingPathComponent:bundleName];
}

基于https://www.cocoawithlove.com/2009/07/temporary-files-and-folders-in-cocoa.html