如何进行随机文件扩展名?

时间:2015-02-06 19:16:46

标签: objective-c cocoa

我在Xcode(mac app)中有一个基于文档的应用程序,我想知道你是否可以拥有它,所以当你将它保存一次它是.xby然后又是另一次它是.rgj或者什么。(随机生成)有可能吗?

回顾:我想在每次使用我的文件时生成随机文件扩展名。我不认为这是可能的。

1 个答案:

答案 0 :(得分:2)

marc说这一切都是代码(主要在SO上找到;))

//from http://stackoverflow.com/questions/2633801/generate-a-random-alphanumeric-string-in-cocoa
-(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;
}
相关问题