NSFileManager 唯一文件名
我需要一种快速、简单的方法来在 iOS 上存储具有唯一文件名的文件。我需要在文件前面加上一个字符串前缀,然后将生成的唯一标识符附加到末尾。我希望 NSFileManager 有一些方便的方法来做到这一点,但我似乎找不到它。
我正在查看 createFileAtPath:contents:attributes:,但不确定属性是否会给我唯一的文件名。
I need a quick and easy way to store files with unique file names on iOS. I need to prefix the file with a string, and then append the generated unique identifier to the end. I was hoping NSFileManager
had some convenient method to do this, but I can't seem to find it.
I was looking at createFileAtPath:contents:attributes:
, but am unsure if the attributes will give me that unique file name.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
创建您自己的文件名:
@darrinm 在评论中提出的更简单的替代方案:
NSLog 输出:
uniqueFileName: 'MyFilename_680E77F2-20B8-444E-875B-11453B06606E-688-00000145B460AF51'
注意:iOS6 引入了 NSUUID 类,可以用来代替 CFUUID。
Create your own file name:
A simpler alternative proposed by @darrinm in the comments:
NSLog output:
uniqueFileName: 'MyFilename_680E77F2-20B8-444E-875B-11453B06606E-688-00000145B460AF51'
Note: iOS6 introduced the NSUUID class which can be used in place of CFUUID.
超级简单的Swift 4 1-liner:
或
Super-easy Swift 4 1-liner:
or
我使用当前日期生成具有给定扩展名的随机文件名。这是我的 NSFileManager 类别中的方法之一:
I use current date to generate random file name with a given extension. This is one of the methods in my NSFileManager category:
您还可以使用古老的
mktemp()
(请参阅man 3 mktemp
)。像这样:XXXXX
将被替换为唯一的字母/数字组合。它们只能出现在模板的末尾,因此您不能在模板中附加扩展名(尽管您可以在获得唯一文件名后附加它)。在模板中添加任意数量的X
。该文件没有创建,需要您自己创建。如果您有多个线程在同一目录中创建唯一的文件,则可能会出现竞争条件。如果是这种情况,请使用 mkstemp() 创建文件并返回文件描述符。
You can also use the venerable
mktemp()
(seeman 3 mktemp
). Like this:The
XXXXX
will be replaced with a unique letter/number combination. They can only appear at the end of the template, so you cannot have an extension appended in the template (though you can append it after the unique file name is obtained). Add as manyX
as you want in the template.The file is not created, you need to create it yourself. If you have multiple threads creating unique files in the same directory, you run the possibility of having race conditions. If this is the case, use
mkstemp()
which creates the file and returns a file descriptor.在 iOS 6 中,最简单的方法是使用:
In iOS 6 the simplest method is to use:
这是我最终在 Swift 3.0 中使用的
Here is what I ended up using in Swift 3.0
Swift 4.1 和 5。只需传递您的文件扩展名,函数就会返回唯一的文件名。
Swift 4.1 and 5. Just pass you file extension name and function will return unique file name.
这可能适合您:
http:// /vgable.com/blog/2008/02/24/creating-a-uuid-guid-in-cocoa/
帖子的作者建议实现“stringWithUUID”方法作为 NSString 的类别。只需将使用此方法生成的 GUID 附加到您正在创建的文件名的末尾即可。
This should probably work for you:
http://vgable.com/blog/2008/02/24/creating-a-uuid-guid-in-cocoa/
The author of the post suggests implementing a 'stringWithUUID' method as a category of NSString. Just append a GUID generated with this method to the end of the file name that you're creating.
Swift 4.2,我使用两个选项,一个大多是唯一但可读的,另一个只是唯一。
Swift 4.2, I use two options, one mostly unique but readable, and the other just unique.