How to edit a plist programmatically? - objective-c

I have a code to edit a plist file. But when the code runs, it changes the plist file but it deletes some other dictionaries. You can look at the images to see what I mean.
To see the edited word, look at dictionary "item 1" and at string "name". You will see that it needs to change from "Second" to "newVALUE".
The original plist is the image of the plist when the plist was created.
Then you have the expected plist which is what the plist should look like. And the edited plist is the plist after the code was applied.
This is the code:
NSString *plistPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
plistPath = [plistPath stringByAppendingPathComponent:#"PassaveData.plist"];
NSMutableArray* newContent = [[NSMutableArray alloc]initWithContentsOfFile:plistPath];
NSMutableDictionary *Dict = [[NSMutableDictionary alloc]initWithDictionary:[newContent objectAtIndex:1]];
[Dict setValue:#"newVALUE" forKey:#"name"];
[Dict writeToFile:plistPath atomically:YES];
These are the images
Click to see the images

Your original plist contains an array of dictionaries. You create a new dictionary and then overwrite the original array-based plist with just the one new dictionary.
You need to update loaded array with the updated dictionary and then write out the whole updated array.
NSMutableArray *newContent = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];
NSMutableDictionary *dict = [newContent[1] mutableCopy];
dict[#"name"] = #"newVALUE";
newContent[1] = dict;
[newContent writeToFile:plistPath atomically:YES];
Note the use of modern syntax for the array and dictionary.

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 write data in .plist?

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];

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.

Saving a NSArray

I would like to save an NSArray either as a file or possibly use user defaults. Here's what I am hoping to do.
Retrieve already saved NSArray (if any).
Do something with it.
Erase saved data (if any).
Save the NSArray.
Is this possible, and if so how should I do this?
NSArray provides you with two methods to do exactly what you want: initWithContentsOfFile: and writeToFile:atomically:
A short example might look like this:
//Creating a file path under iOS:
//1) Search for the app's documents directory (copy+paste from Documentation)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//2) Create the full file path by appending the desired file name
NSString *yourArrayFileName = [documentsDirectory stringByAppendingPathComponent:#"example.dat"];
//Load the array
NSMutableArray *yourArray = [[NSMutableArray alloc] initWithContentsOfFile: yourArrayFileName];
if(yourArray == nil)
{
//Array file didn't exist... create a new one
yourArray = [[NSMutableArray alloc] initWithCapacity:10];
//Fill with default values
}
...
//Use the content
...
//Save the array
[yourArray writeToFile:yourArrayFileName atomically:YES];
You could implement NSCoding on the objects the array contains and use NSKeyedArchiver to serialize/deserialize your array to disk.
BOOL result = [NSKeyedArchiver archiveRootObject:myArray toFile:path];
The archiver will defer to your NSCoding implementation to get serializable values from each object and write a file that can be read with NSKeyedUnarchiver:
id myArray = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
More info in the serialization guide.
This would seem to be a problem most suited to Core Data as this will deal with all the persistent object data. When you retrieve you data it will return an NSSet, which is unsorted so you will have to have some way of sorting the data in the array such as a unique id number assocaited with each object you create.