Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting user input string into a valid file name

What are the special considerations that need to be taken into account when converting a user input string into a valid file name on iOS? Are there any methods available that can help? I couldn't find anything obvious on NSString or NSFileManager.

The context is allowing users to save searches within the app. Behind the scenes, these names are converted into the names of Core Data persistent stores. The actual file names are never exposed to the user.

Thanks in advance.

like image 853
John Topley Avatar asked Oct 22 '22 14:10

John Topley


1 Answers

The primary concern should be the user experience: The user can type anything to identify the file. When coming back to the data she would expect to see exactly the same string she typed in.

The best way to handle this information is to store the actual input somewhere else and use a mapping to get to the actual file.

You could just use a dictionary saved in a plist file. The dictionary would contain the user input as key and a UUID as value. The file is then saved using the UUID as a file name. This way you are sure that the filename is always valid and the user can type whatever she wants without fear for invalid filenames.

An advantage over just stripping invalid characters is that the user can use for instance "/" and "//" as valid identifiers if she feels like it.

like image 102
Nikolai Ruhe Avatar answered Oct 30 '22 15:10

Nikolai Ruhe