How to save a colon character in a file name when using the NSData method "writeToFile: atomically:"?

When I run the following code, the file name that is written to the disk ends as follows: "MyFileName_2011-02-07_13 / 07 / 55.png" . I would like to keep a colon character, not slashes. When I NSLog "fileName" in the console, it looks right. What am I missing?

NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd_HH:mm:ss"]; NSString *dateString = [dateFormatter stringFromDate:[NSDate date]]; NSString *fileName = [NSString stringWithFormat:@"MyFileName_%@.png", dateString]; [myNSData writeToFile:fileName atomically:NO]; 
+4
source share
2 answers

The file name contains a colon; Finder replaces it with a slash.

This is a break when you couldn't use a colon because it was a path separator on Mac OS. Now the path separator is a slash, hence a switch.

The crawler will still not allow you to enter a colon; if you try to enter a slash, this will succeed, but keep the colon name in its place.

Quite a lot of things, including in Cocoa, the colon is valid (not a path separator), but there is no slash.

+7
source

It is usually not possible to have a character : in file names in Darwin (i.e. OS X and iOS). Just try renaming the file on your Mac so that it contains : The writeToFile: method seems to simply replace illegal characters instead of creating and error.

+2
source

Source: https://habr.com/ru/post/1338750/


All Articles