私は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以下)のコンテナを見たとき、ドキュメントディレクトリは空です。
質問:私のファイルはどこに行きましたか?そして私はそれをどのように見つけることができますか?
あなたは、ファイルが実際に書き込まなっていることを確認しましたか? –
それは私がやろうとしていることのようなものです。 ifステートメントを実行すると、ファイルが存在するかのように応答します... – Joe
私の答えはあなたを助けましたか?すべてのコードをコピーして使用してください。 –