私のiPhoneのテキストファイルに自分のログを書きたいと思っています。文字列をファイルに書き込むクイックメソッドを書きました。今は、それをドキュメントディレクトリに保存しています。デバイス上にあれば、私はそれを閲覧するだけでは不便です。私はそれを書いた後、デバイスからすぐにこのファイルを取得する良い方法はありますか?Objective-C:デバイス上にテキストファイルを作成し、簡単にファイルを取得します。
/**
* Logs a string to file
*
* @version $Revision: 0.1
*/
+ (void)logWithString:(NSString *)string {
// Create the file
NSError *error;
// Directory
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"log.txt"];
// Get the file contents
NSData *localData = [NSData dataWithContentsOfFile:filePath];
if (localData) {
NSString *logString = [[NSString alloc] initWithData:localData encoding:NSUTF8StringEncoding];
string = [logString stringByAppendingFormat:@"%@\n", string];
[logString release];
}
// Write to the file
[string writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
}//end
サイドノート - ログファイルが約500KBほど大きくなると、これは本当に非効率になります。 (他のAPIを使用して)変更を書くことのみを考えてください。 – FeifanZ