(IPHONE) Big confusion to read/write the same file from resource folder
i've read several thread for this question but unfortunatly not found the answer to my problem :(
i hav开发者_C百科e a xml file in resource folder, and i need just to re-write the same file; the app logic is:
- display data loaded from file
- 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!
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.
精彩评论