-2

I have a document based application in Xcode (mac app), and I was wondering if you could have it so when you save it one time it's a .xby and then another time it's a .rgj or something.(randomly generated) Is it possible?

Recap: I want to generate a random file extension every time I same my file. I don't think it's possible.

XtremeHek3r
  • 381
  • 2
  • 16
  • 5
    you realize that a filename is just a string? if you can figure out how to generate a random number, you can figure out how to generate a randomized string. – Marc B Feb 06 '15 at 19:18
  • @MarcB actually I want the extension (the .docx or the .mp4, not the name) – XtremeHek3r Feb 06 '15 at 19:19
  • 3
    yes, and the file extension of a filename is a string, just like the rest of the filename... `filename = basename + '.' + generate_random_extension()`. – Marc B Feb 06 '15 at 19:20
  • 1
    Why would you want to do that? – Wain Feb 06 '15 at 19:26

1 Answers1

2

marc said it all but here is the code (found mostly on SO anyways ;))

//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;
}
Daij-Djan
  • 49,552
  • 17
  • 113
  • 135