【发布时间】:2017-08-23 19:14:45
【问题描述】:
我正在为 Apple Watch 编写应用程序。我正在使用以下方法(来自this SE answer)写入日志文件:
- (void) writeLogWith: (NSString *) content {
//Get the file path
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *fileName = [documentsDirectory stringByAppendingPathComponent:@"whathappened.md"];
//create file if it doesn't exist
if(![[NSFileManager defaultManager] fileExistsAtPath:fileName])
[[NSFileManager defaultManager] createFileAtPath:fileName contents:nil attributes:nil];
//append text to file (you'll probably want to add a newline every write)
NSFileHandle *file = [NSFileHandle fileHandleForUpdatingAtPath:fileName];
[file seekToEndOfFile];
[file writeData:[content dataUsingEncoding:NSUTF8StringEncoding]];
[file closeFile];
return;
}
我通过插入手机并直接在手表上运行它来运行它。该函数肯定正在执行(我已经对调试器进行了思考)并且它也知道该文件存在并且不会重复尝试创建它。
Xcode告诉我文件信息是:
Printing description of documentsDirectory:
/var/mobile/Containers/Data/PluginKitPlugin/8503AD6C-6EC9-4522-A867-27109B01B615/Documents
Printing description of documentsDirectory:
(NSString *) documentsDirectory = 0x16d66890
Printing description of fileName:
(NSString *) fileName = 0x16d66950
Printing description of fileName:
/var/mobile/Containers/Data/PluginKitPlugin/8503AD6C-6EC9-4522-A867-27109B01B615/Documents/whathappened.md
我想知道它是否正确地写了东西,但是当我查看容器时(在these SE answers 之后),文档目录是空的。
我的问题是:我的文件去哪儿了?我怎样才能找到它?
【问题讨论】:
-
你检查过文件是否真的被写入了吗?
-
这就是我想要做的。当我单步执行 if 语句时,它会像文件存在一样响应...
-
我的回答对您有帮助吗?尝试复制我所有的代码并使用它。
标签: ios objective-c xcode watchkit