How to make an NSString path (file name) safe

后端 未结 7 719
陌清茗
陌清茗 2021-02-01 01:52

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

相关标签:
7条回答
  • 2021-02-01 02:29

    And of course there's got to be a swift2 guy with an arbitrary hate list (stolen from other answers). That guy is me:

    func sanitizedString(string : String) -> String {
        // put anything you dislike in that set ;-)
        let invalidFsChars = NSCharacterSet(charactersInString: "/* <>?%|")
        let components = string.componentsSeparatedByCharactersInSet(invalidFsChars)
        return components.joinWithSeparator("")
    }
    
    0 讨论(0)
提交回复
热议问题