【问题标题】:How to add timestamp to my filePath?如何将时间戳添加到我的文件路径?
【发布时间】:2014-03-26 18:45:17
【问题描述】:

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

这是我的下载代码:

-(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];
        }
    }];

}

【问题讨论】:

    标签: iphone objective-c timestamp download


    【解决方案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
    

    不完全确定您的意思:

    或者甚至可以为下载文件提供“原始”文件名 实际数组

    【讨论】:

    • 谢谢我试试看。哦,我的意思是原始文件名,就像我的 Parse 数据库中的文件名一样。我正在从那里保存这些文件。
    • 提出“从日期开始没有已知的选择器字符串类”
    • 搞定了!非常感谢,我已经坚持了一段时间。对所有这些东西还是陌生的
    猜你喜欢
    • 1970-01-01
    • 2016-04-19
    • 1970-01-01
    • 2011-02-05
    • 2014-11-06
    • 1970-01-01
    • 2013-08-27
    • 2017-05-26
    • 2023-03-31
    相关资源
    最近更新 更多