NSFileManager unique file names

前端 未结 9 1888
生来不讨喜
生来不讨喜 2020-12-23 10:54

I need a quick and easy way to store files with unique file names on iOS. I need to prefix the file with a string, and then append the generated unique identifier to the en

9条回答
  •  囚心锁ツ
    2020-12-23 11:37

    I use current date to generate random file name with a given extension. This is one of the methods in my NSFileManager category:

    + (NSString*)generateFileNameWithExtension:(NSString *)extensionString
    {
        // Extenstion string is like @".png"
    
        NSDate *time = [NSDate date];
        NSDateFormatter* df = [NSDateFormatter new];
        [df setDateFormat:@"dd-MM-yyyy-hh-mm-ss"];
        NSString *timeString = [df stringFromDate:time];
        NSString *fileName = [NSString stringWithFormat:@"File-%@%@", timeString, extensionString];
    
        return fileName;
    }
    

提交回复
热议问题