【问题标题】:Empty documents directory after creating file with NSFileHandle使用 NSFileHandle 创建文件后清空文档目录
【发布时间】: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


【解决方案1】:

我认为可能会给您带来问题的是NSFileHandle 对象。我已将成千上万个文件写入文档文件夹,但我从未使用过NSFileHandle 来执行此操作。只需使用NSString 上的内置方法将您的字符串写入文件路径。

试试这个:

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 :(");
}

【讨论】:

  • 恐怕这没有什么不同(尽管我确实尝试过)。文件正在写入中,我回去时手表可以找到它,但我只有这些。
【解决方案2】:

从其他研究来看,答案似乎是:

如果您在 Apple Watch 上存储文件,它会存储在它自己的容器中,通过 xcode 是不可见的。

确实,似乎与此错误报告有关:https://openradar.appspot.com/radar?id=5021353337946112,通过此 SE 找到:How to export shared container of an iOS App with Xcode6.2?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-02
    • 1970-01-01
    • 1970-01-01
    • 2014-10-10
    • 2014-09-21
    • 2015-01-30
    • 2016-04-26
    • 1970-01-01
    相关资源
    最近更新 更多