0

我有一个需要能够写入 /var/mobile/Applications/.../Documents 目录的应用程序。这工作正常,但由于某种原因,现在当我运行我的代码时,NSFileManager createFileAtPath 每次都返回 NO。我不知道为什么会发生这种情况,还有其他人吗?我想知道我是否以某种方式意外更改了一些目标设置......

任何帮助表示赞赏,

罗宾

注意:这发生在我尝试写入的任何数据(NSString、NSDictionary 等)

NSDictionary *dataToWrite = [[NSDictionary alloc] initWithObjectsAndKeys:@"1234", @"id", @"hfjk", @"password",nil]; 
NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(

                                                          NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirectory = [arrayPaths objectAtIndex:0];
filePath = [docDirectory stringByAppendingString:@"/sample-file-name"];
NSFileManager *fileMgr;
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:dataToWrite forKey:@"any-key"];
[archiver finishEncoding];

BOOL status = [fileMgr createFileAtPath:path contents:data attributes:nil];
if (status)
    NSLog(@"Created file at path: %@",path);
else
    NSLog(@"Failed to create file at path: %@",path);
4

1 回答 1

0

不写作,

filePath = [docDirectory stringByAppendingString:@"/sample-file-name"];

请检查这个

filePath = [docDirectory stringByAppendingPathComponent:@"sample-file-name"];

请检查NSFileManager 类参考

于 2012-06-20T17:37:44.613 回答