简体   繁体   English

使用NSFileHandle创建文件后清空文档目录

[英]Empty documents directory after creating file with NSFileHandle

I'm writing an application for the Apple watch. 我正在为Apple Watch编写应用程序。 I'm using the following method (from this SE answer ) to write to a log file: 我正在使用以下方法(从SE答复 )来写入日志文件:

- (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;
}

I'm running it by plugging in my phone and running it directly on my watch. 我通过插入手机并直接在手表上运行来运行它。 The function is definitely executing (I've stepped thought with the debugger) and it also knows that the file exists and doesn't repeatedly try and create it. 该函数肯定正在执行(我已经与调试器合作了),并且它也知道该文件存在并且不会反复尝试创建它。

Xcode tells me that the file information is: 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

I'd like to know if it's writing things correctly, but when I look at the container (following these SE answers ), the documents directory is empty. 我想知道它是否写正确,但是当我查看容器时(遵循这些SE回答 ),documents目录为空。 在此处输入图片说明

My question is: where did my file go? 我的问题是:我的文件去了哪里? And how can I find it? 我怎么找到它?

I think what might be giving you problems is the NSFileHandle object. 我认为可能给您带来问题的是NSFileHandle对象。 I have written thousands upon thousands of files to the documents folder and I have never used NSFileHandle to do this. 我已经将成千上万的文件写入documents文件夹,并且从未使用过NSFileHandle Simply use the built in method on NSString to write your string to the file path. 只需使用NSString上的内置方法即可将您的字符串写入文件路径。

Try this: 尝试这个:

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [[documentsDirectory stringByAppendingPathComponent:@"whathappened"] stringByAppendingPathExtension:@"md"];

if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
    [[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil];
}

NSString *string = @"The String You Want To Write.";

NSError *error;
[string writeToFile:filePath atomically:false encoding:NSUTF8StringEncoding error:&error];

if (error) {
    NSLog(@"There was an error writing file\n%@", error.localizedDescription);
}

if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
    NSLog(@"File exists :)");
}
else {
    NSLog(@"File does not exist :(");
}

From other research, the answer appears to be: 根据其他研究,答案似乎是:

If you are storing a file on the Apple Watch, it is stored in it's own container, which isn't visible via xcode. 如果要在Apple Watch上存储文件,该文件将存储在其自己的容器中,该文件无法通过xcode看到。

It indeed, appears to be related to this bug report: https://openradar.appspot.com/radar?id=5021353337946112 , found via this SE: How to export shared container of an iOS App with Xcode6.2? 实际上,它似乎与以下错误报告有关: https : //openradar.appspot.com/radar?id=5021353337946112(通过此SE查找): 如何使用Xcode6.2导出iOS应用程序的共享容器?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM