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

发布于 2024-07-10 02:17:29 字数 139 浏览 9 评论 0原文

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

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

我计划即时写入测试数据,这就是为什么我想将其放入临时目录中。

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

岁月染过的梦 2024-07-17 02:17:29

不要使用 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)];

您现在可以在 temporaryDirectory 内创建文件。 删除生产代码的 NSLogs

Don't use tmpnam() or tempnam(). They are insecure (see the man page for 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:

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 NSLogs for production code.

原谅过去的我 2024-07-17 02:17:29

回顾一下这个问题和 NSTemporaryDirectory() 的文档,如果您使用的是 10.6 或更高版本,那么建议您使用 URLForDirectory:inDomain:propertyForURL: NSFileManager 中的 create:error: 方法提供了更灵活的创建目录的方法。

它返回一个 URL 而不是字符串路径,这是我们建议使用的另一个东西。

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.

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

素染倾城色 2024-07-17 02:17:29

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

In Objective-C you can use NSTemporaryDirectory().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文