그래서 오늘 아침에 아주 이상한 문제가 발생했습니다. 계속 시간이 지남에 따라 계속 증가하는 NSMutableString
을 가지고 있으며 때때로 디스크에 자체 저장하므로 기본적으로 계속 증가하는 텍스트 파일을 만듭니다. 몇 시간 전까지 완벽하게 작동했습니다. 방법 중 어느 것도 코드를 변경하지 않았습니다. content
이 비어있는 동안,NSFileHandle을 올바르게 사용하십시오.
- (void)saveToTextFile {
NSLog(@"SaveTextToFile called");
// Get Current date and time:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd"];
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:@"hh:mm"];
NSDate *now = [[NSDate alloc] init];
NSString *theDate = [dateFormat stringFromDate:now];
NSString *theTime = [timeFormat stringFromDate:now];
NSString *dateToBeDisplayed = [NSString stringWithFormat:@"Date: %@, Time: %@.", theDate, theTime];
// Create text to save, and save it.
stringToSave = [NSString stringWithFormat:@"\n %@ ",dateToBeDisplayed];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"mynotes"];
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];
[fileHandle seekToEndOfFile];
[fileHandle writeData:[stringToSave dataUsingEncoding:NSUTF8StringEncoding]];
[fileHandle closeFile];
// Check what's saved there.
NSString *content = [NSString stringWithContentsOfFile:filePath encoding:NSStringEncodingConversionAllowLossy error:nil];
NSLog(@"File Content: %@", content);
NSLog(@"stringtosave: %@", stringToSave);
}
이상한 부분은, stringToSave
항상 NSLog에서의 적절한 데이터가 :이 NSMutableString
가 stringToSave라고 내가 사용하는 코드입니다. 위의 데이터를 저장하는 방법에 문제가있는 사람이 있습니까?
어쩌면 문제가 될 수 있습니다. 앱을 다시 설치할 때만 시작되기 때문입니다. 흥미 롭 군. 존재하지 않는다면 내가 어떻게 만들 수 있는지 알고 있니? – John
fileExistsAtPath : 및 createFileAtPath : (http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/Reference/Reference.html의 문서 참조) –
굉장합니다. 너는 남자 야, 고마워! – John