1

当我尝试使用此代码写入文件时:

NSString *path = [[NSBundle mainBundle] pathForResource:@"default" ofType:@"txt"];
NSError *error = nil;
NSString*s = @"some text";
BOOL successful = [s writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
NSLog(@"%@",error);
NSLog(@"%d",successful);

什么都没发生。NSLog 说:

(null)
1
4

1 回答 1

2

那是因为您正在编写主捆绑包,请尝试以下操作:

    NSString *applicationDocumentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *storePath = [applicationDocumentsDir stringByAppendingPathComponent:@"default.txt"];

在这里,我们试图覆盖我们的应用程序文档目录。因为你不能在主包上写

于 2013-04-24T20:04:07.230 回答