我想在名为simpleFile.txt
的文档目录中创建一个文件。现在,如果此simpleFile.txt
已存在,我想创建另一个具有相同名称但附加文件编号为simpleFile1.txt
的文件。
答案 0 :(得分:4)
为此目的使用while循环,如下所示
NSFileManager *manager = [NSFileManager defaultManager];
int i = 1;
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [documentPath objectAtIndex:0];
NSString *newFileName = [documentPath stringByAppendingPathComponent:@"simpleFile.txt"];
while ([manager fileExistsAtPath:newFileName]) {
NSString *testName = [NSString stringWithFormat:@"simpleFile%d.txt", i];
newFileName = [documentPath stringByAppendingPathComponent:testName];
i++;
}