V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
wym572
V2EX  ›  iDev

最近开发个小项目,涉及到 Unix 的文件名特殊字符的处理问题

  •  
  •   wym572 · 2017-02-09 23:29:51 +08:00 · 3793 次点击
    这是一个创建于 2629 天前的主题,其中的信息可能已经有所发展或是发生改变。

    需求是某个文件会保存在 Document 文件夹中,这个文件名得是根据用户输入的一串字符(userInput)再加上生成的当前日期(currentDate)组成的。

    但是如果直接用 userInput 的话,比如说里面带有空格,保存之后还按照 userInput+currentDate 拼接打开的话就会报错。

    我希望保存的文件名是用户可辨识的字符串,所以希望只替换部分特殊字符,不知有没有现成的合适的开源库可以自动把影响文件读取的特殊字符替换掉?

    谢谢。

    4 条回复    2017-02-10 08:54:25 +08:00
    wym572
        1
    wym572  
    OP
       2017-02-09 23:49:46 +08:00
    好吧,自己解决了,参考 http://stackoverflow.com/questions/6102333/what-characters-are-allowed-in-a-ios-file-name

    Swift3 下用:
    var invalidCharacters = CharacterSet(charactersIn: ":/")
    invalidCharacters.formUnion(CharacterSet.whitespacesAndNewlines)
    invalidCharacters.formUnion(CharacterSet.illegalCharacters)
    invalidCharacters.formUnion(CharacterSet.controlCharacters)
    let filename = userInput.components(separatedBy: invalidCharacters).joined(separator: "_")
    slixurd
        2
    slixurd  
       2017-02-10 00:14:58 +08:00
    这多麻烦,无脑 Base64 一下简单方便.....
    kitalphaj
        3
    kitalphaj  
       2017-02-10 07:38:23 +08:00
    其实如果不考虑文件名的话完全可以名字是 UUID ,然后把用户输入的名字保存成文件自定义属性。
    hythyt9898
        4
    hythyt9898  
       2017-02-10 08:54:25 +08:00 via iPhone
    正则替换下
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5701 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 50ms · UTC 06:30 · PVG 14:30 · LAX 23:30 · JFK 02:30
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.