2012-10-15 61 views
1

你好,我想創建一個文本文件,然後將存儲一些數據可可和Objective C的保存,編輯和加載文件

- (IBAction)saveUser:(id)sender{ 
    NSString *name = [nameField stringValue]; 
    NSString *weight = [weightField stringValue]; 
    NSDate *date = [datePick dateValue]; 

} 

我希望能夠創建一個存儲以下信息的文件和使用名稱字段作爲文件的名稱。我也希望能夠加載該文件,並從中讀取

任何幫助的數據表示讚賞 感謝你

+0

如果你正在尋找的只是存儲數據,那麼你可以使用coredata儲存它,而不是將其存儲到文本文件 –

+0

也請編輯您的標題以一種有意義的方式.. **可可和目標C **沒有按」牛逼幫助任何人 – Krishnabhadra

+0

請參閱此鏈接[http://klanguedoc.hubpages.com/hub/How-To-Read-and-Write-to-Files-with-IOS-5-SDK-Objective-C-iPad-iPhone-與iPod觸摸](http://klanguedoc.hubpages.com/hub/How-To-Read-and-Write-to-Files-with-IOS-5-SDK-Objective-C-iPad-iPhone-iPod-Touch )這是很好的關於保存/讀取文檔目錄中的文本文件的教程。希望它可以幫助你.. –

回答

3

這是很簡單的。但是你可能想看看NSMutableDictionary。 NSString和Dictionary都有方法writeToFile:@「filename.txt」。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 

NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; 
[dict setValue:[nameField stringValue] forKey:@"name"]; 
[dict setValue:[weightField stringValue] forKey:@"weight"]; 
[dict setValue:date forKey:@"date"]; 

[dict writeToFile:name atomically:YES]; 

您從文件的方式閱讀,

NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:name]; 

由於它得到

0

我想你可以從類的NSFileManager得到一些信息,比如像這樣的簡單,

[文件管理createFileAtPath:文件路徑內容:屬性contentData內容:NULL];

的contentData是NSData的,它包括你要保存的內容,文件名是在您需要設置文件路徑。

謝謝!