【问题标题】:Delete a file from the partial string xcode从部分字符串 xcode 中删除文件
【发布时间】:2015-06-23 02:12:21
【问题描述】:

我正在寻找助手。在我的情况下,我有诸如

之类的文件
  1. RN150622103444544_pr.pdf
  2. RN150622103444544_ID_GD.pdf
  3. RN150622103444544_CA.xml

我的问题是如何仅通过引用 RN150622103444544 来删除所有文件。 目前我的代码正在一一删除。 以下是我的示例代码

-(void)deleteOldPdfs:(NSString *)proposal
{


    NSError *error = nil;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

 if([fm fileExistsAtPath:[FormsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@_pr_1.pdf",proposal]]])
{
        [fm removeItemAtPath:[FormsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@_pr_1.pdf",proposal]] error:&error];
}
    if([fm fileExistsAtPath:[FormsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@_id_GD.pdf", proposal]]]){
        [fm removeItemAtPath:[FormsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@_id_GD.pdf", proposal]] error:&error];
    }
    if([fm fileExistsAtPath:[FormsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@_CA.xml", proposal]]]){
        [fm removeItemAtPath:[FormsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@_CA.xml",proposal]] error:&error];
}
}

提前致谢

【问题讨论】:

    标签: ios objective-c iphone xcode string


    【解决方案1】:

    您可以将唯一的后缀放在一个数组中,然后遍历该数组。

    示例代码(未经测试):

    NSArray *suffixList = @[@"pr_1.pdf", @"id_GD.pdf", @"CA.xml"];
    
    NSError *error = nil;
    NSFileManager *fm = [NSFileManager defaultManager];
    
    for (NSString *suffix in suffixList) {
        NSString *path = [FormsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@_%@",proposal, suffix]];
        if([fm fileExistsAtPath:path]){
            [fm removeItemAtPath:path error:&error];
        }
    }
    

    注意:pathsdocmentsDirectory 未在问题代码中使用,也不包含在答案中。

    【讨论】:

    • 嗨 zaph,如果我想通过查看 RN150622103444544 来删除并忽略后面的类型...请您帮助我...谢谢
    • 使用:if (fileName hasPrefix:@"RN150622103444544")
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多