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]];
Related
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"]];
I am a little confused while using NSDictionary. I have a array (nameArray), I add that array to a Dictionary (nameDict) and finally I add this dictionary to another dictionary (requestDict).
NSMutableArray *nameArray = [[NSMutableArray alloc] initWithObjects:#"abcd",#"lmnop",#"xyz",#"pqr", nil];
NSMutableDictionary *nameDict = [NSMutableDictionary alloc] initWithObjectsAndKeys:nameArray,#"name", nil];
NSDictionary *requestDict = [NSDictionary dictionaryWithDictionary:nameDict];
When I remove objects from nameDict , requestDict is not affected.
[nameDict removeAllObjects]
So far so good. But my query is when I remove objects from nameArray why responseDict is affected.
[nameArray removeAllObjects];
Why nameArray still has objects. Shouldn't it have been deallocated as soon as I remove objects from nameDict. Please help me understand if I am missing something here.
What happens if I set nameArray to nil in this case?
Both nameDict and requestDict have a reference to the single array pointed to by nameArray. Changes made to nameArray are seen by both dictionaries.
When you did [nameArray removeAllObjects] you see the change to the array in both dictionaries because both dictionaries are referencing the one copy of the mutable array.
If you set nameArray to nil, nothing happens. Both dictionaries still have a reference to the mutable array.
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.
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.
I want to make array of float type.
Anybody can help me?
NSArray *arrOfFloat = [[[NSArray alloc]initWithObjects:[12.2, 23.44], nil]];
But i want to make array dynamically.
But i want to make array dynamically.
This means you'll have to use an NSMutableArray.
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:#1.1];
[array addObject:#2.2];
...
You also can't add primitives to an array. You'll need to add objects. Notice the # I added before the numbers. This creates number literals.
If you have the floats you want to add as variables, you can auto boxing like this:
[array addObject:#(myFloatVariable)];
Use NSMutableArray class instead of a NSArray (this is a subclass of it), this way, within your code, you will be able to call :
NSMutableArray *yourArray = [NSMutableArray new];
[yourArray addObject:#(1.0f)];
NSArraycan only store objects, so in your case you would have to store your float as NSNumber. If you want to store objects dynamically, thus adding or removing them to an NSArray you have to use the mutable object type called NSMutableArray.
You'll need to wrap your float's in an NSNumber:
[NSNumber numberWithFloat:12.2];
If you're dynamically adding elements to an array, you need to use NSMutableArray.
NSMutableArray *array = [NSMutableArray array];
[array addObject:[NSNumber numberWithFloat:12.2]];
You can use NSMutableArray for example named arrOfFloat and add this :
[arrOfFloat addObject:[NSNumber numberWithFloat:3.5]];
[arrOfFloat addObject:[NSNumber numberWithFloat:23.44]];
Hope, It will may helpful to you.