【问题标题】:How to avoid overwriting files on iPhone Documents folder?如何避免覆盖 iPhone Documents 文件夹中的文件?
【发布时间】:2023-03-07 03:31:01
【问题描述】:

我需要将包含NSArrays 的文件写入我的 iPhone 应用程序的 Documents 文件夹中。下次我需要创建一个新文件,而不是覆盖以前的文件。

我试过这样,但它只写doc0doc1

allDocs 在其他地方声明的NSArray 中。

怎么了?谢谢!

 NSString *myDoc;
 NSString *temp;

  for (int i = 0; i < [allDocs count]; ++i){
    NSFileManager* fileMgr = [NSFileManager defaultManager];
    myDoc = [NSString stringWithFormat:@"doc%d.dat", i];
    NSString* currentFile = [documentsDirectory stringByAppendingPathComponent:myDoc];
    BOOL fileExists = [fileMgr fileExistsAtPath:currentFile];
    if (fileExists == NO){
        temp = [NSString stringWithFormat:@"doc%d.dat", i];
        break;
    } else {
        temp = [NSString stringWithFormat:@"doc%d.dat",i++];
        break;
    }
}

NSString *myArray = [documentsDirectory stringByAppendingPathComponent:myDoc];

NSMutableArray *myMutableArray = [[NSMutableArray alloc] initWithContentsOfFile: myArray];

if(myMutableArray == nil)

{

    myMutableArray = [[NSMutableArray alloc] initWithCapacity:10];

    myMutableArray = anotherArray; 
} 

[myMutableArray writeToFile:myArray atomically:YES];

【问题讨论】:

  • 进入for循环之前[allDocs count]的值是多少?
  • 它会有所不同,例如从 1 到 20 或更多。感谢您的帮助!
  • 用 currentDateTime 保存 FileName 和秒可以为您节省一些麻烦。 stackoverflow.com/a/10671095/1378447

标签: iphone objective-c cocoa-touch nsstring nsarray


【解决方案1】:

无论是否找到文件,每次都在第一次迭代时跳出 for 循环。您应该循环使用 i 的递增值,直到 fileExists 为 false。

- (BOOL) workFileExists:(NSString *)name {
    NSFileManager *fm = [NSFileManager defaultManager];
    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filename = [name stringByAppendingString:@".swrk"];

    return [fm fileExistsAtPath:[path stringByAppendingPathComponent:filename]];
}

- (NSString *)uniqueUntitledName {
    NSString *untitled = @"Untitled";
    NSString *name = untitled;
    int i = 1;
    while ([self workFileExists:name]) {
        name = [NSString stringWithFormat:@"%@%d", untitled, i];
        i++;
    }
    return name;
}

【讨论】:

  • 我应该改用 continue 吗?
  • 我已经用我在项目中使用的一些代码更新了答案。它的命名风格略有不同,但应该作为起点。
  • 感谢代码,因为我已经在 else 语句中尝试过 this,但它从较高的数字开始并不断减少,直到达到零 while (fileExists == NO) { temp = [NSString stringWithFormat :@"doc%d.dat",i++]; }
【解决方案2】:
UIImage *imageForShare = [UIImage imageNamed:@"anyImage.jpg"];
NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"New Folder"];
// New Folder is your folder name
NSError *error = nil;

 //in this method you are checking the path is exist or not for Folder Name
if (![[NSFileManager defaultManager] fileExistsAtPath:stringPath])
[[NSFileManager defaultManager] createDirectoryAtPath:stringPath withIntermediateDirectories:NO attributes:nil error:&error];

//now checking the image name already exist or not

NSString *fileName = [stringPath stringByAppendingFormat:@"/image.jpg"];
if (![[NSFileManager defaultManager] fileExistsAtPath:fileName])
{
  NSLog(@"Path is available.");
  NSData *data = UIImageJPEGRepresentation(imageForShare, 1.0);
  [data writeToFile:fileName atomically:YES];
}
else
{
    NSLog(@"Path doesn't exist, same name of image is already exist.");
}

谢谢!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-30
    • 1970-01-01
    • 2015-07-10
    • 1970-01-01
    • 2014-06-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多