What characters are allowed in a iOS file name?

后端 未结 9 2142
长情又很酷
长情又很酷 2020-12-29 05:20

I\'m looking for a way to make sure a string can be used as a file name under iOS. I\'m currently in the section of the code that deletes incompatible characters. I\'m wonde

9条回答
  •  南笙
    南笙 (楼主)
    2020-12-29 05:59

    I came up with the following solution. Works nice so far.

    import Foundation
    
    extension String {
        func removeUnsupportedCharactersForFileName() -> String {
            var cleanString = self
            ["?", "/", "\\", "*"].forEach {
                cleanString = cleanString.replacingOccurrences(of: $0, with: "-")
            }
            return cleanString
        }
    }
    
    let a = "***???foo.png"
    let validString = a.removeUnsupportedCharactersForFileName()
    

提交回复
热议问题