2012-07-09 66 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]; 
} 
相关问题