macos 如何获取 Mac OS X 上的默认临时目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/374431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:59:26  来源:igfitidea点击:

How do I get the default temporary directory on Mac OS X?

objective-cmacoscocoatest-datatemporary-directory

提问by Abizern

I'd like to create some directories of data for some unit tests and I'd like these directories to be in the default temporary directory for the user.

我想为某些单元测试创​​建一些数据目录,并且我希望这些目录位于用户的默认临时目录中。

I could just create a subdir under /tmp I suppose, but I don't want to make an assumption about how somebody has set up their own machine.

我想我可以在 /tmp 下创建一个子目录,但我不想假设有人如何设置自己的机器。

I'm planning on writing the test data on the fly, which is why I'd like to put this into a temporary directory.

我打算即时编写测试数据,这就是我想将其放入临时目录的原因。

采纳答案by Abizern

Looking back at this question there and the documentation for NSTemporaryDirectory(), if you are using 10.6 or above, then you are recommended to use the URLForDirectory:inDomain:appropriateForURL:create:error:method from NSFileManager for a more flexible method of creating directories.

回头看看这个问题和 NSTemporaryDirectory() 的文档,如果你使用的是 10.6 或更高版本,那么建议你使用NSFileManager 中的 URLForDirectory:inDomain:properForURL:create:error:方法以获得更灵活的创建目录的方法.

And it returns a URL instead of a string path, which is another thing we're recommended to use.

它返回一个 URL 而不是字符串路径,这是我们推荐使用的另一件事。

回答by Dave Dribin

Don't use tmpnam()or tempnam(). They are insecure (see the man pagefor details). Don't assume /tmp. Use NSTemporaryDirectory()in conjunction with mkdtemp(). NSTemporaryDirectory()will give you a better directory to use, however it can return nil. I've used code similar to this:

不要使用tmpnam()tempnam()。它们是不安全的(有关详细信息,请参阅手册页)。不要假设/tmp。使用NSTemporaryDirectory()会同mkdtemp()NSTemporaryDirectory()会给你一个更好的目录来使用,但是它可以返回nil. 我使用过类似的代码:

NSString * tempDir = NSTemporaryDirectory();
if (tempDir == nil)
    tempDir = @"/tmp";

NSString * template = [tempDir stringByAppendingPathComponent: @"temp.XXXXXX"];
NSLog(@"Template: %@", template);
const char * fsTemplate = [template fileSystemRepresentation];
NSMutableData * bufferData = [NSMutableData dataWithBytes: fsTemplate
                                                   length: strlen(fsTemplate)+1];
char * buffer = [bufferData mutableBytes];
NSLog(@"FS Template: %s", buffer);
char * result = mkdtemp(buffer);
NSString * temporaryDirectory = [[NSFileManager defaultManager]
        stringWithFileSystemRepresentation: buffer
                                    length: strlen(buffer)];

You can now create files inside temporaryDirectory. Remove the NSLogsfor production code.

您现在可以在temporaryDirectory. 删除NSLogsfor 生产代码。

回答by Abizern

In Objective-C you can use NSTemporaryDirectory().

在 Objective-C 中,您可以使用 NSTemporaryDirectory()。

回答by Jasper Bekkers