0

我想将我的应用程序数据保存在文档文件夹中的文本文件中我使用以下方式存储但它存储单个字符串我希望我的数据在数组中如何将数组的所有内容存储在文本文件中

  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *documentsDirectory = [paths objectAtIndex:0];
  NSString*yourString=@"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;
4

2 回答 2

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];
}
于 2012-07-09T06:29:06.810 回答
0

在 NSMutableString 中添加完整的数组数据,然后存储在文本文件中。

于 2012-07-09T06:20:28.980 回答