如何制作随机文件扩展名?
How do I make a random file extension?
我在 Xcode(mac 应用程序)中有一个基于文档的应用程序,我想知道您是否可以拥有它,所以当您保存一次时它是一个 .xby 然后另一次它是 .rgj 或其他东西。(随机生成)这可能吗?
回顾:我想在每次相同文件时生成一个随机文件扩展名。我觉得不可能。
marc 都说了,但这是代码(反正主要是在 SO 上找到的 ;))
//from
-(NSString *) randomStringWithLength: (int) len {
static NSString *letters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
NSMutableString *randomString = [NSMutableString stringWithCapacity: len];
for (int i=0; i<len; i++) {
[randomString appendFormat: @"%C", [letters characterAtIndex: arc4random_uniform((u_int32_t)letters.length)]];
}
return randomString;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
id basename = @"myFile";
id extension = [self randomStringWithLength:3];
id filename = [basename stringByAppendingPathExtension:extension];
NSLog(@"%@", filename);
return YES;
}
我在 Xcode(mac 应用程序)中有一个基于文档的应用程序,我想知道您是否可以拥有它,所以当您保存一次时它是一个 .xby 然后另一次它是 .rgj 或其他东西。(随机生成)这可能吗?
回顾:我想在每次相同文件时生成一个随机文件扩展名。我觉得不可能。
marc 都说了,但这是代码(反正主要是在 SO 上找到的 ;))
//from
-(NSString *) randomStringWithLength: (int) len {
static NSString *letters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
NSMutableString *randomString = [NSMutableString stringWithCapacity: len];
for (int i=0; i<len; i++) {
[randomString appendFormat: @"%C", [letters characterAtIndex: arc4random_uniform((u_int32_t)letters.length)]];
}
return randomString;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
id basename = @"myFile";
id extension = [self randomStringWithLength:3];
id filename = [basename stringByAppendingPathExtension:extension];
NSLog(@"%@", filename);
return YES;
}