文档目录中的读/写文件问题 [英] Read/write file in Documents directory problem
问题描述
我正在尝试将一个非常基本的文本字符串写入我的文档目录,然后从那里开始工作,以便稍后保存其他文件等。
I am trying to write a very basic text string to my documents directory and work from there to later save other files etc.
我目前仍然坚持使用它将任何内容写入我的文档目录
I am currently stuck with it not writing anything into my Documents directory
(在我的 viewDidLoad
中)
NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)
NSString *documentsDirectory = [pathArray objectAtIndex:0];
NSString *textPath = [documentsDirectory stringByAppendingPathComponent:@"file1.txt"];
NSString *text = @"My cool text message";
[[NSFileManager defaultManager] createFileAtPath:textPath contents:nil attributes:nil];
[text writeToFile:textPath atomically:NO encoding:NSUTF8StringEncoding error:NULL];
NSLog(@"Text file data: %@",[[NSFileManager defaultManager] contentsAtPath:textPath]);
这是打印出来的:
2011-06-27 19:04:43.485 MyApp [5731:707]文本文件数据:(null)
2011-06-27 19:04:43.485 MyApp[5731:707] Text file data: (null)
如果我试试这个,它也打印出null:
If I try this, it also prints out null:
NSLog(@"My Documents: %@", [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:NULL]);
在写这个文件时我错过了什么或者我做错了什么?
我需要在我的plist或者所需的一些框架/进口中改变它吗?
What have I missed or am I doing wrong while writing to this file? Might it be something I need to change in my plist or some frameworks/imports needed?
谢谢
我通过writeToFile传递了一个NSError对象并收到此错误:
I passed a NSError object through the writeToFile and got this error:
错误:错误Domain = NSCocoaErrorDomain Code = 512操作无法完成。(Cocoa错误512.)UserInfo = 0x12aa00 {NSFilePath = / var / mobile / Applications / 887F4691-3B75-448F-9384-31EBF4E3B63E / Documents / file1.txt,NSUnderlyingError = 0x14f6b0操作无法完成。不是目录}
Error: Error Domain=NSCocoaErrorDomain Code=512 "The operation couldn’t be completed. (Cocoa error 512.)" UserInfo=0x12aa00 {NSFilePath=/var/mobile/Applications/887F4691-3B75-448F-9384-31EBF4E3B63E/Documents/file1.txt, NSUnderlyingError=0x14f6b0 "The operation couldn’t be completed. Not a directory"}
这在模拟器上工作正常,但在我的手机上没有:/
This works fine on the simulator but not on my phone :/
推荐答案
通过在info.plist中设置非标准捆绑ID来解决问题
The problem got solved by setting a non standard Bundle ID in die info.plist
我使用iTunes Connect中的Bundle ID来获取此特定应用。现在一切都很完美。
I used the Bundle ID from iTunes Connect for this specific app. Now everything works perfectly.
这篇关于文档目录中的读/写文件问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!