What characters are allowed in a iOS file name?

后端 未结 9 2137
长情又很酷
长情又很酷 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 06:04

    This String extension (Swift 4.2) will help convert an invalid iOS file name to a valid iOS file name.

     extension String {
        func convertToValidFileName() -> String {
            let invalidFileNameCharactersRegex = "[^a-zA-Z0-9_]+"
            let fullRange = startIndex..

    For example

    "name.name?/!!23$$@1asd".convertToValudFileName()       // "name-name-23-1asd"
    
    "!Hello.312,^%-0//\r\r".convertToValidFileName()        // "-Hello-312-0-"
    
    "/foo/bar/pop?soda=yes|please".convertToValidFileName() // "-foo-bar-pop-soda-yes-please"
    

提交回复
热议问题