2012-07-09 65 views
0

我想保存在文本文件中我的應用程序數據文件夾 我使用下面的方法來存儲,但它存儲單個字符串我想,我的數據是在數組怎麼可能陣列中的所有內容存儲在文本文件中保存數據從iPhone應用程序在iPhone應用程序的文本文件

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString*[email protected]"This is working fine"; 
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"file.txt"]; 
    NSString *stringWithoutSpaces = [yourString stringByReplacingOccurrencesOfString:@" " withString:@""]; 
    [stringWithoutSpaces writeToFile:filePath atomically:TRUE encoding:NSUTF8StringEncoding error:NULL]; 

我的數組就像下面

DataController *coffeeObj = [appDelegate.coffeeArray objectAtIndex:indexPath.row]; 




cell.resturantLocationLabel.text=coffeeObj.resturantLocation; 
    cell.foodQualityRatingLabel.text=coffeeObj.foodQualityRating; 
    cell.foodPresentationRatingLabel.text=coffeeObj.foodPresentationRating; 
    cell.waiterRatingLabel.text=coffeeObj.waiterRating; 
    cell.ambienceRatingLabel.text=coffeeObj.ambienceRating; 
    cell.overallRatingLabel.text=coffeeObj.overallRating; 
    cell.commentsLabel.text=coffeeObj.comments; 
    cell.emailAddressLabel.text=coffeeObj.emailAddress; 
    cell.addtoMailingListLabel.text=coffeeObj.addtoMailingList; 


    NSString*test=coffeeObj.comments; 
+0

什麼是數組數據?我們可以有樣品嗎? – 2012-07-09 06:17:49

回答

0

中的NSMutableString添加一系列完整的數據,然後在文本文件存儲。

+0

你能告訴我如何添加數組的NSMutableString – user1495149 2012-07-09 06:42:32

+0

NSMutaleString * _STR = = [[的NSMutableString的alloc]初始化] for(int i = 0; i Ayaz 2012-07-09 06:57:36

+0

看到我更新的代碼,我有對象的數組如何我可以用這個喜歡你給我想把所有的內容 – user1495149 2012-07-09 07:12:48

0
// Here you set your text.  
NSString *yourAppendingText = @"yourAppendingText"; 

// Here you get access to the file in Documents directory of your application bundle. 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentDir = [paths objectAtIndex:0]; 
NSString *documentFile = [documentDir stringByAppendingPathComponent:@"file.txt"]; 

// Here you read current existing text from that file 
NSString *textFromFile = [NSString stringWithContentsOfFile:documentFile encoding:NSUTF8StringEncoding error:nil]; 

// Here you append new text to the existing one if it is 
if(textFromFile){ 
    NSLog(@"appending"); 
    NSString *textToFile = [textFromFile stringByAppendingString:[NSString stringWithFormat:@" %@",yourAppendingText]]; 
    // Here you save the updated text to that file 
    [textToFile writeToFile:documentFile atomically:YES encoding:NSUTF8StringEncoding error:nil]; 

} 
else{ 

    NSLog(@"not appending"); 
    [yourAppendingText writeToFile:documentFile atomically:YES encoding:NSUTF8StringEncoding error:nil]; 
} 
相關問題