我需要在應用程序執行時存儲某些信息,並在應用程序啓動時再次獲取它。我嘗試使用GData將其存儲爲XML,但沒有成功。 我使用NSFileHandle它不會給我一個錯誤,但它無法創建一個.txt文件的讀/寫目的。有沒有其他的方式來存儲和檢索iPhone上的數據。以下是我的NSFileHandle代碼。iPhone永久存儲器
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"myFile.txt"];
//NSFileHandle *fh = [NSFileHandle fileHandleForWritingAtPath:@"file://localhost/Users/shraddha/Desktop/info.txt"];
NSFileHandle *fh = [NSFileHandle fileHandleForWritingAtPath:@"myFile.txt"];
[fh seekToEndOfFile];
NSData *data = [camName dataUsingEncoding:NSASCIIStringEncoding];
[fh writeData:data];
[fh closeFile];
對於閱讀
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"myFile.txt"];
//NSFileHandle *fh = [NSFileHandle fileHandleForReadingAtPath:@"file://localhost/Users/shraddha/Desktop/info.txt"];
NSFileHandle *fh = [NSFileHandle fileHandleForReadingAtPath:@"myFile.txt"];
if(fh == nil)
return nil;
else
{
NSData *data = [fh readDataOfLength:8];
NSString *retStr = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
return retStr;
}