如何使NSString路径(文件名)安全 [英] How to make an NSString path (file name) safe
问题描述
我使用非常棘手的战斗方法:)使像'Fi?le * / Name'这样的字符串安全使用作为文件名,如'File_Name'。
我敢肯定有一个可可的方式来转换它。我确定最好的地方是这里:)
I'm using very tricky fighting methods :) to make a string like 'Fi?le*/ Name' safe for using as a file name like 'File_Name'. I'm sure there is a cocoa way to convert it. And I'm sure the best place to ask is here :)
谢谢!
推荐答案
除非你显式地运行shell或通过使用诸如 popen
或系统
,没有理由转义除了路径名分隔符之外的任何内容。
Unless you're explicitly running the shell or implicitly running the shell by using a function such as popen
or system
, there's no reason to escape anything but the pathname separator.
您可能还希望强制文件名不以句号开头会导致Finder隐藏文件),并且也应该强制它不是空的,并且少于 NAME_MAX
个字符*长。
You may also want to enforce that the filename does not begin with a full stop (which would cause Finder to hide the file) and probably should also enforce that it is not empty and is fewer than NAME_MAX
characters* long.
* syslimits.h
说字节,但如果你通过文件管理器,它是字符。我不知道哪个是适合Cocoa的。
*syslimits.h
says bytes, but if you go through File Manager, it's characters. I'm not sure which is right for Cocoa.
这篇关于如何使NSString路径(文件名)安全的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!