【发布时间】:2023-03-07 03:31:01
【问题描述】:
我需要将包含NSArrays 的文件写入我的 iPhone 应用程序的 Documents 文件夹中。下次我需要创建一个新文件,而不是覆盖以前的文件。
我试过这样,但它只写doc0 和doc1。
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