Edit an existing plist in iOS - ios

Good Morning,
I am creating my first iphone app so bear with me.
I have created a plist and have attached a picture so you can see how I set it up.
I'm trying to add another Item under Clients, which has 3 keyvaluepairs in it.
This is what I have so far but I am not understanding how to edit the saved plist I have.
NSString *plistCatPath = [[NSBundle mainBundle] pathForResource:#"Property List" ofType:#"plist"];
NSDictionary *clientDictionary = [[NSDictionary alloc] initWithContentsOfFile:plistCatPath];
NSMutableArray *arrayTempClients = [[NSMutableArray alloc] initWithObjects:clientDictionary[#"Clients"], nil];
NSMutableArray *array1 = [[NSMutableArray alloc] initWithArray:arrayTempClients[0]];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
dict[#"Description"] = #"testAccount";
dict[#"Username"] = #"customUsername";
dict[#"Password"] = #"customPassword";
[array1 addObject:dict];
any help would be greatly appreciated!

You can not make any changes to the app and that includes files in it.
If you need a version of the plist you can edit then on first launch copy the plist file to your app's Document directory. Then you can read it into a NSMutableDictionary, add the new data and save it back.

Related

iOS - Add NSDictinary key values into an NSArray sequencialy

I have a NSDictionary and a NSArray:
self.dataDic = [[[NSDictionary alloc] init] autorelease];
self.dataDicKey = [[[NSArray alloc] init] autorelease];
These are the key and value of my NSDictionary:
self.dataDic =
#{#"Ring Cloud" :#[#"Contact Cloud", #"Image Cloud", #"Data Cloud"],
#"Ring Tools" :#[#"Sticker Market", #"Live Poll", #"Mail"],
#"Ring Apps" :#[#"Studio", #"Player"]};
Now what I want is to insert all the key value of my self.dataDic into self.dataDicKey sequentially. That means it looks like this:
self.dataDicKey = [[[NSArray alloc] initWithObjects:#"Ring Cloud", #"Ring Tools", #"Ring Apps",nil] autorelease];
I have tried with this:
self.imageDicKey = [[[self.imageDic allKeys] reverseObjectEnumerator] allObjects];
But it's not actually sorted the values. If any one have any suggestion please share it with me.
Thanks a lot in advance.
If you are trying to get the keys as objects in your NSArray you should try this method
self.DataDicKey = [[NSArray alloc]initWithArray:[self.imageDic allKeys]];
One thing to remember that NSDictionary allKeys method returns only the keys regardless of order they were saved. as its KVP
This will copy your NSDictionary keys into NSArray
NSDictionary keys are unsorted - maybe you could save the keys in array before you enter the data to the dictionary

Do NSArrays stay sorted when saved then retrieved from a plist

Simple question do NSArrays stay sorted when saved to a plist and then retrieved?
I am having a sorting issue when i retrieve my array from my plist so i was curious.
I think it may also be a problem with this line. Do these maintain the order from titles followed the order of titleArr?
NSMutableArray *allTitles = [[NSMutableArray alloc] init];
[allTitles addObjectsFromArray:titles];
[allTitles addObjectsFromArray:titleArr];
How I save to plist
imageInfo = [[NSMutableArray array] init];
NSMutableArray *Listing = [NSMutableArray array];
[imageInfo addObject:Listing];
NSDictionary *Images = [NSDictionary dictionaryWithObjectsAndKeys:
allTitles, kKeyTitle,
allMediaUrls, kKeyThumbUrl,
allWidths, kKeyThumbWidth,
allHeights, kKeyThumbHeight,
nil];
[Listing addObject:Images];
NSArray *sysPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory ,NSUserDomainMask, YES);
NSString *documentsDirectory = [sysPaths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"image.plist"];
NSLog(#"Plist File Path: %#", filePath);
BOOL didWriteToFile = [imageInfo writeToFile:filePath atomically:YES];
if (didWriteToFile) {
NSLog(#"Write to .plist file is a SUCCESS!");
} else {
NSLog(#"Write to .plist file is a FAILURE!");
}
Yes if titles and titleArr are sorted than allTitles will also be sorted.
First all objects of titles will be added and than at all objects titleArr will be added as addObjectsFromArray method starts adding object array towards the end of receiving array.
Here is more info from Apple Docs
Adds the objects contained in another given array to the end of the
receiving array’s content.
So yes plist will have sorted array.
Severe bug:
imageInfo = [[NSMutableArray array] init];
[NSMutableArray array] returns an initialised and autoreleased array.
Sending an init message to that array is not a good idea.

how can i create plist like below in image?

Iam trying to create a plist with following feature.
Inside plist there are two keys
1.id
2.status
current values are id:91 status:buy here Id should be unique .Means if i tried to add a 3rd entry like id:91 status:downlaod .Then it should replace the existing 91 and status:buy with new one.
please help me
check below image
How can i remove the status and id if i got one match in Id?
Enumerate root array and compare id value in dictionary
NSMutableArray *rootArray = [[NSMutableArray alloc] initWithCapacity:0];
NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:#"91",#"id",#"Buy",#"status",nil];
NSDictionary *dic1 = [[NSDictionary alloc] initWithObjectsAndKeys:#"92",#"id",#"Download",#"status",nil];
[rootArray addObject:dic];
[rootArray addObject:dic1];
NSDictionary *dic3 = [[NSDictionary alloc] initWithObjectsAndKeys:#"91",#"id",#"Download",#"status",nil];
NSDictionary *foundMatch =nil;
for(NSDictionary *dictionary in rootArray)
{
if ([[dictionary objectForKey:#"id"] isEqualToString:[dic3 objectForKey:#"id"]]) {
foundMatch = dictionary;
break;
}
}
if (foundMatch!=nil) {
[rootArray removeObject:foundMatch];
[rootArray addObject:dic3];
}
else {
[rootArray addObject:dic3];
}
[rootArray writeToFile:#"path" atomically:YES];
It is .plist file which has array of dictionary and each dictionary has two key
id
status
I give you official documentation for How to create plist file programmatically.

NSMutableDictionary giving all together same values instead of new fresh values

i am constructing All data to look like a response data dictionary from a server.
Now, newsFeedsDict1 which should Dictionary for both Bolly and Global is not only showing all data inside Global dictionary only. While my for loop is running its showing correct data for Bolly. but for 2nd time its showing Bolly's data also in Global dictionary.
if(internetStatus == NotReachable)
{
NSMutableArray *titleArr = [[NSMutableArray alloc] init];
NSMutableArray *wholeFeeds = [[[NSMutableArray alloc] init] autorelease];
[titleArr addObject:#"Bolly"];
[titleArr addObject:#"Global"];
for (NSString *title in titleArr) {
//titleArr = [[NSUserDefaults standardUserDefaults] objectForKey:#"TitleArray"];
NSLog(#"TITle arr %#",titleArr);
NSLog(#"No internet");
OrderedDictionary *newsFeedsDict1 = [[[OrderedDictionary alloc] init] autorelease];
NSMutableDictionary *newsFeedsDict = [[[NSMutableDictionary alloc] init] autorelease];
NSMutableArray *myLocalArray= [[[NSMutableArray alloc] init] autorelease];
myLocalArray = [[Database sharedDatabase] getArticleData:title];
NSMutableDictionary *articleDict = [[[NSMutableDictionary alloc] init] autorelease];
[articleDict setObject:myLocalArray forKey:#"article"];
[newsFeedsDict setObject:articleDict forKey:#"Articles"];
[newsFeedsDict setObject:title forKey:#"#name"];
[newsFeedsDict1 setObject:newsFeedsDict forKey:title];
[wholeFeeds addObject:newsFeedsDict1];
NSLog(#"news feed dict %#",newsFeedsDict1);
NSMutableDictionary *temparticleDictionary = [[NSMutableDictionary alloc] init];
self.articleDictionary = temparticleDictionary;
self.categoriesDictionary = [[NSMutableDictionary alloc] init];
self.categoriesDictionary =newsFeedsDict1;
[self createArticleDictionaryForCategory:newsFeedsDict];
}
}
It's difficult to tell what your code is supposed to do, and how you can tell that one dictionary has the same content as another. A couple problems:
NSMutableArray *myLocalArray= [[[NSMutableArray alloc] init] autorelease];
myLocalArray = [[Database sharedDatabase] getArticleData:title];
The first line is entirely unnecessary. You're creating a new array, assigning it to myLocalArray, and then assigning something else to myLocalArray. You do the same later with self.categoriesDictionary. This leads me to believe that you have some misunderstanding with respect to object pointers.
So, the array that you get from your shared database ends up at myLocalArray, and you then add that array to the dictionary at articleDict, and then add articleDict to newsFeedDict and in turn add that to newsFeedDict1. And then you log newsFeedDict1. You do exactly the same for both your titles, #"Bolly" and #"Global", so it's not at all surprising that you see the same output in both cases.
I'm having a hard time seeing why you expect the output to be different, and I have to guess that again it's due to a misunderstanding of what happens when you assign one object pointer to another. Or, perhaps you're expecting the array that you get from [[Database sharedDatabase] getArticleData:title]; to be different because you're passing in different titles. Maybe you really should be getting different arrays there; it would be a good idea to look at what happens in that -getArticleData: method and whether it really is giving you the right results for each title.
If that doesn't help, take some time to clean up your code so that it's easier for us, and more importantly, for you to really see what's going on. Also, please edit your question to give a better description of what you're seeing and how that's different from what you expect to see.
Can you write the snippet for *getArticleData()* method

Read Mutable Array from File / Plist

Here is my problem. I show files out of an MutableArray in a UITableView with UISearchBar.
Therefore I made an Array:
allTableData = [[NSMutableArray alloc] initWithObjects:
[[CRFParts alloc] initWithName:#"ABC" andDescription:#"DEF"],
[[CRFParts alloc] initWithName:#"GHI" andDescription:#"JKL"],
nil];
I have round about 2000 lines of this. I build me simple strings out of an excel file but the code inside my .m is huge than. Is there a possibility to read these content out of an file (plist/txt)?
Thanks for helping!
Try looking at This Question. It has a lot of good code on how to read a plist and dictionaries vs arrays.
And next time, search before you ask.
You can easely use:
NSArray *array = [[NSArray alloc] initWithContentsOfURL:url];
or NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfURL:url];
Check out Apple Documentation on the topic.
Be careful on what structure you are loading as a plist can represent and array or a dictionary at its root. Other thread about it

Resources