NSMutableDictionary Removes same values from different array - ios

Hi i am using the following code to remove objects from NSMutableDictionary, both dictionaries contains same array values, if i remove a value from D1 the same value get removed from D2 Automatically.
Help me out how to solve this,
NSMutableDictionary *D1=[[NSMutableDictionary alloc]init];
NSMutableDictionary *D2=[[NSMutableDictionary alloc]init];
NSMutableArray *arr_objs = [[NSMutableArray alloc]initWithObjects:#"ss",#"nn", nil];
[D1 setObject:arr_objs forKey:#"Keys"];
[D2 setObject:arr_objs forKey:#"Keys"];
[[D1 objectForKey:#"Keys"]removeObject:#"nn"];

arr_objs is the same array in two dictionaries. This
NSMutableArray *arr_objs = [[NSMutableArray alloc]initWithObjects:#"ss",#"nn", nil];
NSMutableArray *arr_objsCopy = [arr_objs mutableCopy];
[D1 setObject:arr_objs forKey:#"Keys"];
[D2 setObject:arr_objsCopy forKey:#"Keys"];
should give you what you're looking for. Rather than storing the same array in two dictionaries, this example creates two identical arrays that can later be modified without affecting each other.

Related

How to add two array's objects as objects and keys to the third array in Objective C

I am trying to add two arrays into the third array i.e one array's objects are values to the third array and other array's objects are keys to the third array and I am getting the output as null , Is this a right way to do.
NSArray *newcontactkeys,*newcontactvalues ;
NSMutableArray *autoSyncDataArray;
newcontactkeys=[self.resultDict objectForKey:#"keys"];
newcontactvalues=[self.resultDict objectForKey:#"values"];
[autoSyncDataArray setValue:[self.resultDict objectForKey:#"values"]
forKey:[self.resultDict objectForKey:#"Keys"]];
NSLog(#"autosyncArray is %#",autoSyncDataArray);
Output: autosyncArray is (null)
You are using an array, which is an indexed collection, but talking about keys, dictionaries are keyed collections.
NSDictionary has a class method to directly create a dictionary from two arrays, use this and your code becomes, updating to modern Obj-C syntax:
NSArray *newContactKeys = self.resultDict[#"keys"];
NSArray *newContactValues = self.resultDict[#"values"];
NSDictionary *autoSyncDataDict = [NSDictionary dictionaryWithObjects:newContactValues
forKeys:newContactsKeys];
HTH
You're not initialising your array.
NSMutableArray *autoSynchDataArray = [NSMutableArray new]
Also use addObject instead of set value:
[autoSyncDataArray addObject:[self.resultDict objectForKey:#"values"]
You cannot join/Manipulate multiple Array of type NSArray.
Create a NSMutableArray which allows reordering and manipulation of data in it. You can add any number of objects to it, rearrange it and delete it.
NSMutableArray *templateArray = [NSMutableArray new];
There are 2 issues:
Null issue: You are not initializing array. So you will have to use below statement:
NSMutableArray *autoSyncDataArray = [[NSMutableArray alloc] init];
Key/Value Pair: If you want to put keys and values in an array. It will not work like Dictionary. It will go into sequential form. If you are ok with sequential form then use:
[autoSyncDataArray addObject:[self.resultDict objectForKey:#"keys"]];
[autoSyncDataArray addObject:[self.resultDict objectForKey:#"values"]];
otherwise use:
NSMutableDictionary *autoSyncDataDict = [[NSMutableDictionary alloc] init];
[autoSyncDataArray setValue:[self.resultDict objectForKey:#"values"]
forKey:[self.resultDict objectForKey:#"Keys"]];

Magic command for extracting an array of names from an array of NSPropertyDescription objects

I have this array with a bunch of NSPropertyDescription objects. These objects have a property called name.
I want to extract an array containing just the names of all these objects.
Ok, I can do this:
NSMutableArray *array = [[NSMutableArray alloc] init];
for (NSPropertyDescription *property in anEntity) {
[array addObject:property.name];
}
but I know objective-c has a lot of magical commands to extract stuff from arrays of objects.
How do I do that using one of those magical commands?
thanks.
Key-Value Coding should do the trick:
NSArray *names = [arrayOfPropertyDescriptions valueForKey:#"name"];
For an array, valueForKey returns an array containing the results of invoking valueForKey: using the key on each of the array's objects.

How to populate an array with dictionaries containing certain key/values

I'm working with a plist file at the moment but intend to switch over to json when the backend is finally built. So for the moment my plist is an array that contains a bunch of dictionaries.
I'd like to use this information to create a new array containing only the dictionaries with certain values.
For example. My plist contains a bunch of locations like so:
key: location value:example place name here
key: type value:indoor
I want to build an array containing only those with "indoor" set as the type value.
And then perhaps a second one containing all "outdoor" locations.
What's the best way to go about doing this, or perhaps I can be directed to a tutorial of some sort.
Thanks.
Simply loop through your array and add the qualifying dictionaries to a new array.
NSMutableArray *arrayIndoor = [NSMutableArray array];
NSMutableArray *arrayOutdoor = [NSMutableArray array];
NSString *type;
for (NSDictionary *dict in arrayPList) {
type = [dict objectForKey:#"type"];
if ([type isEqualToString:#"indoor"])
[arrayIndoor addObject:dict];
else if ([type isEqualToString:#"indoor"])
[arrayOutdoor addObject:dict];
}
All you are really needing to do is sort the array into two arrays. There isn't a direct method that I have seen that will do this for you. My suggestion would be to use a fast enumeration over the array and conditionally break it into two new arrays.
NSMutableArray *locations = [[NSMutableArray alloc] init];
NSMutableArray *type = [[NSMutableArray alloc] init];
for (NSDictionary *dict in MyPlistArray) {
if ([dict valueForKey:#"locationKey"]) {
[locations addObject:dict];
} else if ([dict valueForKey:#"typeKey"]) {
[type addObject:dict];
}
}
You might need to use a different method for determining which key to put in each array, but you get the general idea.
Also I'm assuming that you would want the arrays of dictionaries to persist after, so you can just set those up as properties instead of local variables.

How can I alphabetically sort the values in my NSDictionary with an NSArray?

I have an NSDictionary which contains several arrays with several strings in them. I want to put all strings under each array in one single array. How can I receive all the strings at once? I've tried this:
NSMutableArray *mutarr = [[NSMutableArray alloc] initWithObjects:[self.firstTableView allValues], nil];
NSArray *mySorted = [[NSArray alloc]init];
mySorted = [mutarr sortedArrayUsingSelector:#selector(caseInsensitiveCompare:)];
NSLog(#"First table view values: %#", mySorted);
NOTE: self.firsttableview is a NSDictionary like this:
NSString *path = [[NSBundle mainBundle] pathForResource:#"firstTableView" ofType:#"plist"];
self.firstTableView = [NSDictionary dictionaryWithContentsOfFile:path];
EFFECT: This gives me a list in NSLog, but it isn't in alphabetic order.
You are initializing your mutarr with one object which is the array returned by allValues on your dictionary. Instead try this:
NSMutableArray* mutarr = [NSMutableArray array];
for (NSArray* array in self.firstTableView.allValues) {
[mutarr addObjectsFromArray:array];
}
Your code is going to mean that mutarr is an array containing an array of arrays, not an array of strings. You should be looping over the array values from the dictionary and adding the items from each one to your mutarr to make it an array of strings that you can sort.
If your NSDictionary values are arrays then you are trying to sort the arrays instead of the strings. Try to create a for loop to iterate [self.firstTableView allValues] then add all values in that array to your main to sort array.

NSMutableArray getting changed

I have an NSMutableArray (detailsMA)having 100 strings like #"True".
Am storing that array in NSMutableDictionary which is already allocated.
NSArray *array1 = [[NSArray alloc] initWithArray:detailsMA];
[detailsDictionary setObject: array1 forKey:#"detailsMA"];
Now i changed the values of detailsMA, like replacing strings to #"False", After that am retrieving the original array values,
[detailsMA removeAllObjects];
[detailsMA addObjectsFromArray:[detailsDictionary objectForKey:#"detailsMA"]];
Here am getting modified values, not the original values.
I need original values.
Please help me.
You should copy the array.
NSArray *array1 = [[NSArray alloc] initWithArray:detailsMA copyItems:YES];
Don't forget to implement the NSCopying protocol in the classes for the objects to be copied
EDIT:
When you add the objects again, make a copy before adding.
[detailsMA addObjectsFromArray:[[NSArray alloc] initWithArray:[detailsDictionary objectForKey:#"detailsMA"] copyItems:YES]];

Resources