如何为我的filePath添加时间戳?

时间:2014-03-26 18:45:18

标签: iphone objective-c timestamp download

我有一个下载文件夹的静态文件路径,文件下载很好,但是当我下载一个新文件时,旧文件被新文件替换。我想因为静态路径。如何为我的代码添加时间戳,以便每当新的下载时,旧的下载都不会被替换?或者甚至可以从实际的数组中提供下载文件的“原始”文件名?

这是我的下载代码:

-(void) Savefile {

    [self.downloadfile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
        if (error)  {

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Wooopss!" message:@"Download failed. Try Again..." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
        }
        else if (data) {


            [data writeToFile:@"/Users/Danny/Desktop/PDFFile/hello.pdf" atomically:NO ];



            NSLog(@"Downloading file...");

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Downloading" message:@"File is being downloaded..." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
        }
    }];

}

1 个答案:

答案 0 :(得分:1)

NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:@"MMddYYYYmmss"];

NSString *filePath = [NSString stringWithFormat:@"/Users/Danny/Desktop/PDFFile/hello_%@.pdf", [dateFormatter stringFromDate:[NSDate date]]];

哪个会给你这个:

/Users/Danny/Desktop/PDFFile/hello_032620144401.pdf

不完全确定你的意思:

  

或者甚至可以给下载文件"原文"来自的文件名   实际数组