Convert user input string to a valid file name

What are some special considerations to consider when converting a user input string to a valid filename in iOS? Are there any methods available? I could not find anything obvious in NSStringor NSFileManager.

Context allows users to save searches in the application. Behind the scenes, these names translate to the names of the persistent storage of Core Data. Actual file names are never displayed to the user.

Thanks in advance.

+5
source share
3 answers

The main problem is the user interface: the user can enter anything to identify the file. Returning to the data, she expected to see exactly the same line into which she typed.

The best way to handle this information is to save the actual entry to another location and use the mapping to get the actual file.

You can simply use the dictionary stored in the plist file. The dictionary will contain user input as a key and a UUID value. The file is then saved using the UUID as the file name. Thus, you are sure that the file name is always valid, and the user can enter whatever he wants, without fear for invalid file names.

, , , , , "/" "//" , .

+3

, . , Core Data ? "" .

, , , , .

+1

. - :

filename = [[filename componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] componentsJoinedByString:@"" ];
filename = [[filename componentsSeparatedByCharactersInSet:[NSCharacterSet illegalCharacterSet]] componentsJoinedByString:@"" ];
filename = [[filename componentsSeparatedByCharactersInSet:[NSCharacterSet symbolCharacterSet]] componentsJoinedByString:@"" ];
fileURLString = [NSTemporaryDirectory() stringByAppendingPathComponent:filename];
fileURL = [NSURL URLWithString:fileURLString];

, . - :

[[NSFileManager defaultManager] fileExistsAtPath:[fileURL absoluteString]]
0

All Articles