How to make an NSString path (file name) safe

后端 未结 7 740
陌清茗
陌清茗 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:23

    Solution in Swift 4

    extension String {
        var sanitizedFileName: String {
            return components(separatedBy: .init(charactersIn: "/\:\?%*|\"<>")).joined()
        }
    }
    

    Usage:

    "https://myurl.com".sanitizedFileName // = httpsmyurl.com
    

提交回复
热议问题