2017-08-23 3 views
0

私はAppleの時計用のアプリケーションを作成しています。私は、ログファイルに書き込む(this SE answerから)次のメソッドを使用しています:NSFileHandleでファイルを作成した後の空のドキュメントディレクトリ

- (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以下)のコンテナを見たとき、ドキュメントディレクトリは空です。 enter image description here

質問:私のファイルはどこに行きましたか?そして私はそれをどのように見つけることができますか?

+0

あなたは、ファイルが実際に書き込まなっていることを確認しましたか? –

+0

それは私がやろうとしていることのようなものです。 ifステートメントを実行すると、ファイルが存在するかのように応答します... – Joe

+0

私の答えはあなたを助けましたか?すべてのコードをコピーして使用してください。 –

答えて

0

私はあなたに問題を与えるかもしれないと思うが、NSFileHandleオブジェクトです。私は何千ものファイルを書類フォルダに書いてきましたが、これを行うには一度もNSFileHandleを使っていません。文字列をファイルパスに書き込むには、NSStringの組み込みメソッドを使用します。

これを試してみてください:

あなたはアップルウォッチにファイルを格納している場合、「にISNれ、それ自身のコンテナに格納されます。他の研究から

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

これはいいえ、違い(私はそれを試していたが)を恐れている。ファイルが書き込まれており、私が戻ったときに時計がそれを見つけることができますが、それは私が持っているすべてです。 – Joe

関連する問題