How to write data in .plist? - objective-c

I'm trying to save some comments in a plist, that's OK cause its just a prototype. The problem is that i can read from plist but when I try to write and read after that, it throws an "array out of bounds" exception. I can't figure it out what I'm doing wrong here.
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"Comments" ofType:#"plist"];
NSMutableArray *plistArray = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
NSMutableDictionary *newComment = [NSMutableDictionary dictionary];
[newComment setValue:commentTitle.text forKey:#"title"];
[newComment setValue:comment forKey:#"comment"];
[plistArray addObject:newComment];
[plistArray writeToFile:filePath atomically:NO];
That works fine, then i try to read:
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"Comments" ofType:#"plist"];
NSMutableArray *plistArray = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
NSMutableDictionary *dictionary = (NSMutableDictionary *) [plistArray objectAtIndex:0];
NSLog(#"%#", [dictionary objectForKey:#"title"]);
And it throws the exception.
If I add the item manually to the plist, it works fine, i guess it means that my reading code its fine.
Could it be the structure of my plist?
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
</array>
</plist>
* Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFArray objectAtIndex:]: index (1) beyond bounds (1)'
I added the "description" to the array before writing to the plist. If i use the following code:
NSString *aDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
// NSString *aFilePath = [NSString stringWithFormat:#"%#/Comments.plist", aDocumentsDirectory];
//
// NSMutableArray *plistArray = [[NSMutableArray alloc] initWithContentsOfFile:aFilePath];
The return is (null)
But if i use:
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"Comments" ofType:#"plist"];
NSMutableArray *plistArray = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
i can see the contents of the array, and its all working properly.
The problem is: In both ways i cant write to the file, it keeps returning "NO". And i already checked the permissions

You are trying to write the file into mainBundle. Definitely not possible.
You will have to write the plist file to Documents or Application Support folder of the app.
Create File Path in Documents Directory :
NSString *aDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *aFilePath = [NSString stringWithFormat:#"%#/Comments.plist", aDocumentsDirectory];
Write to FilePath
[plistArray writeToFile:aFilePath atomically:YES];
Read From FilePath
NSMutableArray *plistArray = [[NSMutableArray alloc] initWithContentsOfFile:aFilePath];

I see two problems with your code:
(May or may not be a problem). If the file does not exist initially, the initWithContentsOfFile: selector will return nil, causing the rest of your code to be no-ops.
(Probably the cause). You may not write to the bundle resources directory. Store your file in the Documents or Caches directory instead.
To locate your documents directory, use something like this:
- (NSString*) pathForDocument:(NSString*)documentName {
NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
if(documentDirectories.count < 1) return nil;
return [[documentDirectories objectAtIndex:0] stringByAppendingPathComponent:documentName];
}

First of all, why are you writing a file into your bundle?
Then, to address your problem, check if you actually did write the file.
if ([plistArray writeToFile:filePath atomically:NO])
NSLog (#"Written");
else
NSLog (#"Not Written");
Also, log your array when you're read it using -(void)description to check the contents of the dictionary.
Edit
As you said that you're not writing to your plist. For now, just create a test plist on your desktop.
NSString *testPath = [[NSString stringWithString:#"~/Desktop/Comments.plist"] stringByExpandingTildeInPath];
if ([plistArray writeToFile:testPath atomically:NO])
NSLog (#"Written");
else
NSLog (#"Not Written");
If that still returns Not Written, then there's something wrong with your dictionary. Which I doubt because it's just strings (Though they could be placeholders for asking your question on stackoverflow. The docs states that the classes in the dictionary must be of NSData, NSDate, NSNumber, NSString, NSArray, or NSDictionary). If that says written though, I'm guessing it doesn't write to your bundle because of permissions, which then you have to change your plist location to somewhere else other than your bundle, which I highly recommend.

If you only put one item in the array, you should obviously use index 0 instead of 1 when reading from it:
NSMutableDictionary *dictionary = (NSMutableDictionary *) [plistArray objectAtIndex:0];

Related

How can i update plist file which has many group item in Objective-C?

I have a plist file like this, and now i want to edit the row.
For example, i want to edit row "ZFILMSEAT" in Item 1.
Please somebody suggest me how to update data in plist file.
First of all, When you retrieve the .plist file, store it as NSMutableDictionary.
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"your plist name" ofType:#"plist"];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
NSMutableArray *filmsPlaying = [[NSMutableArray alloc] iniWithArray:[dict objectForKey:#"FilmsPlaying"]];
NSMutableDictionary *filmToEdit = [filmsPlaying objectAtIndex:1];// IRL run a loop to get your desired film
NSString *newSeats = #"1-2-9";
[filmToEdit setObject:newSeats forKey:#"ZFILMSEAT"];
[filmsPlaying replaceObjectAtIndex:1 withObject:filmsToEdit];
[dict setObject:filmsPlaying forKey:#"FilmsPlaying"];
Here you have the film you want to edit. After you have made your edition to that dictionary, you need to write it back to App's document dir. (NOTE: You CANNOT save it back to your main bundle but you can write it to Documents instead).
NSString *pathForPlist = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
pathForPlist = [savingPath stringByAppendingPathComponent:#"your plist name.plist"];
[dict writeToFile:savingPath automically:YES];
To edit it in the main bundle without any programming, you can simply open it in any text editor or xcode itself and make your edition. Nothing techie about that.

NSMutableArray addObjects NSString

I have an problem with NSString and NSMutableArray
I Have:
NSString *msgID;
NSMutableArray *mArray;
and msgID string is an unique ID for every message and it changes it self every time when you receive a new message.
and now i want to save those IDS into NSMutableArray to put them inside plist file.
but the problem is when i do like the following
a = [[NSMutableArray alloc] init];
[a addObject:msgID];
it save only the first ID not the rest of theme
Example if the output ID is 65465465151 and you received new message after one second with ID 2123545445 the NSMutableArray save only the first output.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<string>65465465151</string>
</array>
</plist>
how can I make NSMutableArray add all outputs or strings which already output using one NSString ?
Here is my code
NSString *msgID = [viewcontroller.messageID substringFromIndex:[viewcontroller.messageID length] - 21];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"msgIDs.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSMutableArray *mArray;
if (![fileManager fileExistsAtPath:path]) {
path = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat: #"msgIDs.plist"] ];
}
if ([fileManager fileExistsAtPath:path]) {
mArray = [[NSMutableArray alloc] init];
[mArray addObject:msgID];
} else {
// If the file doesn’t exist, create an empty dictionary
mArray = [[NSMutableArray alloc] init];
}
[mArray writeToFile:path atomically:YES];
If that's your code, and it's all in one block like that and not spread over several methods, in several different loops, then you're creating a new instance of mArray every time you go through the code block, and, as a result, at most there will be one entry in the array.
Your code looks OK, provided you are not trying to create the array in a loop repeatedly. Each addObject will enlarge the array by one. Make sure you write your plist file after the array is populated the way you expect. Make sure any old plist file is overwritten.

How to edit elements in plist file

I have a Settings.plist and I want to edit some values in this file.
My function to edit/writing is:
- (void) setParamWithName: (NSString*) Name withValue: (NSString*) Value {
// get paths from root direcory
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
// get documents path
NSString *documentsPath = [paths objectAtIndex:0];
// get the path to plist file
NSString *plistPath = [documentsPath stringByAppendingPathComponent:#"Settings.plist"];
// check to see if Data.plist exists in documents
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath])
{
// if not in documents, get property list from main bundle
plistPath = [[NSBundle mainBundle] pathForResource:#"Settings" ofType:#"plist"];
}
// read property list into memory as an NSData object
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
NSString *errorDesc = nil;
NSPropertyListFormat format;
// convert static property list into dictionary object
NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc];
if (!temp)
{
NSLog(#"Error reading plist: %#, format: %d", errorDesc, format);
}
// checking if element exists, if yes overwriting
// if element not exists adding new element
[temp writeToFile:plistPath atomically:YES];
}
This function read and write (with te same values) Settings.plist.
I do not have any idea (my knowledge about objective-c is not enough) how to add new element or edit existing element. Can anyone help mi with this issue?
I think it's easier as you think.
Once you got the path of the file read it into a NSDictionary. Make a mutable copy of that dictionary with mutableCopy and NSMutableDictionary.
Now edit that mutable dictionary as you like (add s.th., remove s.th., edit s.th. and so on).
Now that you're done you can write it back to the old path as you did with temp.
Your main problem is that you're not working with a mutable version of that dicitionary. It'd make your life much easier.

Cannot change mutable array?

Ok so I have been stuck on this for a while even though it's a simple problem, I am trying to add an NSDictionary to an array however when calling the addObject method on the array the program crashes claiming I am sending a mutating method to an immutable object.
my code looks like:
- (IBAction)btnSaveMessage:(id)sender {
//Save The Message and Clear Text Fields
NSMutableDictionary *newMessageDictionary = [[NSMutableDictionary alloc] init];
[newMessageDictionary setObject:#"plist test title" forKey:#"Title"];
[newMessageDictionary setObject:#"plist subtitle" forKey:#"Subtitle"];
[newMessageDictionary setObject:#"-3.892119" forKey:#"Longitude"];
[newMessageDictionary setObject:#"54.191707" forKey:#"Lattitude"];
NSMutableArray *messagesArray =[[NSMutableArray alloc]init];
//Load Plist into array
NSString *messagesPath = [[NSBundle mainBundle] pathForResource:#"Messages"
ofType:#"plist"];
messagesArray = [NSArray arrayWithContentsOfFile:messagesPath];
[messagesArray addObject:newMessageDictionary]; //this causes crash
//write messagesarray to file
NSString *plistPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES) lastObject];
plistPath = [plistPath stringByAppendingPathComponent:#"usersMessages.plist"];
[messagesArray writeToFile:plistPath atomically:YES];
So I understand that I am trying to add to what the compiler see's as an immutable array but I declared it as Mutable?
Whats going on? :(
You are re-initializing messagesArray with
[NSArray arrayWithContentsOfFile:messagesPath]
Making it not mutable. Try:
[NSMutableArray arrayWithContentsOfFile:messagesPath];
You are overwriting your NSMutableArray with an NSArray in this line
messagesArray = [NSArray arrayWithContentsOfFile:messagesPath];
The class method arrayWithContentsOfFile: is returning just an NSArray and not an NSMutableArray.
If you want the content of the file mutable you can do this:
NSMutableArray *messagesArray = [[NSArray arrayWithContentsOfFile:messagesPath] mutableCopy];
and you can remove the previous declaration
NSMutableArray *messagesArray =[[NSMutableArray alloc]init];
now.

NSMutableArray always return null

My problem is when I read content of plist file in an NSMutableArray always return null
NSString *resourceDocPath = [[NSString alloc] initWithString:[[NSBundle mainBundle]bundlePath]] ;
// Create the new dictionary that will be inserted into the plist.
NSMutableDictionary *nameDictionary = [NSMutableDictionary dictionary];
[nameDictionary setValue:#"walid" forKey:#"id"];
[nameDictionary setValue:#"555 W 1st St" forKey:#"lien"];
NSString *r = [NSString stringWithFormat:#"%#/download.plist", resourceDocPath];
NSLog(#"%#",r);
// Open the plist from the filesystem.
NSMutableArray *plist = [NSMutableArray arrayWithContentsOfFile:r];
NSLog(#"%#",plist);
if (plist == NULL)
{
plist = [NSMutableArray array];
}
[plist addObject:nameDictionary];
NSLog(#"%#",plist);
[plist writeToFile:r atomically:YES];
when I look in the plist file I found the data that I insert only one
can you help me please?
You're trying to access the application bundle rather than the documents directory, which can be accessed via NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"Populator"];. The bundle cannot be modified, so the created array is never saved, hence why it is never loaded.
First you should not check for plist == null but check for plist == nil
Second searching for the download file should be changed into the following:
NSURL *url = [[NSBundle mainBundle] URLForResource:#"download" withExtension:#"plist"];
NSMutableArray *plist = [NSMutableArray arrayWithContentsOfURL:url];
Third:
I do not think a file with the extension of plist will return an Array.
It will probably represent an dictionary. Try creating an NSMutableDictionary instead of an array.