开发者

writeToFile Output .plist not writing correct values

开发者 https://www.devze.com 2023-01-04 17:25 出处:网络
I\'ve been trying to write a simple shopping list program but when i try to write the modified values back to a plist file i only get the original values that were in the file to start with.

I've been trying to write a simple shopping list program but when i try to write the modified values back to a plist file i only get the original values that were in the file to start with.

My original plist file is one large array (dataArray) of dictionaries with only two fields in each dictionary one bool and one string when you select an item in the tableview the bool value is changed for the single item and when i output the dataArray to the console i can see that the value has been changed, my only problem is that when i try to write dataArray to a file i get the file in the documents directory but all of the bool values are still off.

- (void)viewDidLoad {

 //load from plist file
 NSString *path = [[NSBundle mainBundle] pathForResource:@"OriginalChecklist" ofType:@"plist"];
 self.dataArray = [NSMutableArray arrayWithContentsOfFile:path];

    [super viewDidLoad];
}

- (void)viewWillDisappear:(BOOL)animated 
{
 NSString *documentsDirectory = [@"~/Documents/" stringByExpandingTildeInPath];
 NSString *savePath = [documentsDirectory stringByAppendingPathComponent:@"Checklist.plist"];
 [dataArray writeToFile:savePath atomically:YES];

 NSLog(@"location information %@", savePath); 
 NSLog(@"dataAray contents %@=", dataArray);
}

And as i said when i output the result of the dataArray to the console i get the following results from the items i checked.

cell = <UITableViewCell: 0x3b143d0; frame = (0 0; 320 44); text = 'Water Bottle'; autoresize = W; layer = <CALayer: 0x3b14650>>;
checked = 1;
text = "Water Bottle";

cell = <UITableViewCell: 0x3b20560; frame = (0 88; 320 44); text = 'GPS'; autoresize = W; layer = <CALayer: 0x3b20610>>;
checked = 1;
text = GPS;

If anyone knows how to save the modified results to the plist file i would much appreciate a little help.

Thanks Brad


I'm still having a little trouble with my application being able to create a plist and load the information from that plist in the ViewDidLoad section. The code i'm using at the moment to check for the original file and if it is not there create one is as follows

(void)viewDidLoad {

BOOL success;

NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"CustomChecklist.plist"];

success = [fileManager fileExistsAtPath:filePath];
NSLog(@"STATUS OF SUCCESS %@",success);
if (!success) {
    NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingFormat:@"OriginalChecklist.plist"];
    success = [fileManager copyItemAtPath:path toPath:filePath];
    self.dataArray = [NSMutableArray arrayWithContentsOfFile:filePath];
    NSLog(@"IF STATEMENT CREATING THE FILE");
}else {
    self.dataArray = [NSMutableArray arrayWithContentsOfFile:filePath];开发者_开发技巧
    NSLog(@"IF STATEMENT READING THE FILE");
}

NSLog(@"location information %@", filePath);
[super viewDidLoad];

}

When I build this application it builds and runs ok but I get a couple of warnings NSFileManager may not respond to -copyItemAtPath:toPath

Once this has completed the output to the console looks as though it hasn't even checked for the file or created the one to read from.

2010-07-06 16:22:42.803 CampingChecklist[400:207] STATUS OF SUCCESS (null) 2010-07-06 16:22:42.805 CampingChecklist[400:207] IF STATEMENT READING THE FILE 2010-07-06 16:22:42.814 CampingChecklist[400:207] location information /Users/Brad/Library/Application Support/iPhone Simulator/3.1.3/Applications/E7074D5A-3E7A-4D2E-9F92-82B1A04873F8/Documents/CustomChecklist.plist

And obviously as there is nothing in the file, when it tries to save on the way out of the application it doesn't create any file in the path specified.

If anyone can shed any light on this problem then I would much appreciate it.

Thanks Brad


When you load your plist for the first time, it comes from the App Bundle - which is not writeable.

When you save your data, you're correctly saving it into the documents directory.

However, when you load your plist again the next time, you're loading it from the Appe Bundle again which hasn't been changed.

You need to put some logic into your plist loading code that checks for the existance of the file in the documents directory. If it exists, load it, if not load the one from the app bundle.

0

精彩评论

暂无评论...
验证码 换一张
取 消