views:

385

answers:

1

hi all, i've read several thread for this question but unfortunatly not found the answer to my problem :(

i have a xml file in resource folder, and i need just to re-write the same file; the app logic is:

  1. display data loaded from file
  2. add new data to the same file

for read a file my code is:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"myfile" ofType:@"xml"];  
NSData *dataXml=[NSData dataWithContentsOfFile:filePath];

for re-write the same file:

NSString *myDataString = @"?xml version=""1.0"" encoding=""UTF-8""?";
NSString *path = [[NSBundle mainBundle] pathForResource:@"myfile" ofType:@"xml"];  
BOOL ok=[myDataString writeToFile:path atomically:YES encoding:NSUnicodeStringEncoding error:nil];

For read works fine, but when i try to write, the file is not updated, but it was created another one in another folder, i've tested this in the simulator noting that an app is in a folder like this:

Users/user/Library/Application Support/iPhoneSimulator/Users/Applications/05279766-7B2C-4FC0-BFB0-D87A158A3337/

where there are 3 folder (Documents,tmp,Libary) and the bundle file .app (where there is my xml file)

My question are: is my logic wrong? in wich way are managed app folders in the device??

thanks in advance and sorry for the long thread!

+4  A: 

You cannot write files into your application bundle; it's read only (on the device, at least; you may be able to do this in the Simulator).

What you should do is use your ~/Documents folder for the file. If it's not there, first read it from your bundle, then write it out to ~/Documents, and from then on, read it from there.

Ben Gottlieb
thank you for your relpy Ben!...ok, but when i call writeToFile on the same path where before i've read, my BOOL is true; so in where folder(simulator/device) file is saved?where could i see the ~/Documents folder in xcode?
Mat
~/Documents is the Documents folder relative to your app; for example, Users/user/Library/Application Support/iPhoneSimulator/Users/Applications/05279766-7B2C-4FC0-BFB0-D87A158A3337/Documents
Ben Gottlieb