I have a variable declared as NSDictionary. How can I copy it to an extern const NSArray variable? For example:
NSMutableDictionary *selectedItem = [self.dataArray objectAtIndex:indexPath.row];
[selectedItem setObject:[NSNumber numberWithBool:targetCustomCell.checked]
forKey:#"checked"];
allKeys returns an NSArray containing all the keys. allValues returns an NSArray containing all the values.
NSArray *keys = [myDict allKeys];
NSArray *values = [myDict allValues];
Related
I have one NSDictionary and one NSMutableArray And I want store many object from NSDictionary into NSMutableArray from one key but I dont know about it.
this is my code :
//NSArray * a = [[NSArray alloc]initWithObjects:#"1",#"2",#"3",#"4",#"7",#"9", nil];
//NSArray *b = [[NSArray alloc]initWithObjects:#"1",#"2",#"3",#"4",#"5",#"0", nil];
NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:a,#"number1",b,#"number2", nil];
NSMutableArray *array = [NSMutableArray alloc].... ?
I want store object in number1 key from NSDictionary in NSMutableArray
Since it appears that you store arrays as NSDictionary elements, you can do this:
NSMutableArray *array = [[NSMutableArray alloc] initiWithArray:dic[#"number1"]];
or
NSMutableArray *array = [NSMutableArray arrayWithArray:dic[#"number1"]];
To add object into NsDictionary Array do:
NSMutableDictionary * datos = [array objectAtIndex:i];
[datos setObject:#"hola" objectForKey#"newKey"];
Webservice sends me JSON that looks like that:
[
{"key":"key1","value":"value1"},
{"key":"key2","value":"value2"},
{"key":"key3","value":"value3"}
]
How should I set up RestKit mapping to get the following NSDictionary?
#{ #"key1" : #"value1", #"key2" : #"value2", #"key3": #"value3" }
This may help you, try this.
NSDictionary *dict1 = [[NSMutableDictionary alloc] init];
NSArray *arr = [[NSArray alloc] initWithArray:<Parse the array here from RESTkit>];
for (int i = 0;i < [arr count], i++)
{
NSDictionary *dict = [arr objectAtIndex:i];
NSString *key = [dict objectForKey:#"key"];
NSString *value = [dict objectForKey:#"value"];
[dict1 setObject:value forKey:key];
}
NSLog(#" Dictionary %#", dict1);
[dict1 release];
I'm new to Obj-C, and I have a little problem here.
I'm trying insert the values from a temp Dictionary into an array
NSMutableDictionary *myDict = [[NSMutableDictionary alloc] init];
NSMutableArray *myArray = [[NSMutableArray alloc] init];
[myDict setObject:[NSNumber numberWithInt:100] forKey:#"Record"];
[myDict setObject:#"www" forKey:#"Name"];
[myArray addObject:myDict];
[myDict setObject:[NSNumber numberWithInt:80] forKey:#"Record"];
[myDict setObject:#"eee" forKey:#"Name"];
[myArray addObject:myDict];
[myDict setObject:[NSNumber numberWithInt:70] forKey:#"Record"];
[myDict setObject:#"rrr" forKey:#"Name"];
[myArray addObject:myDict];
[myDict setObject:[NSNumber numberWithInt:0] forKey:#"Record"];
[myDict setObject:#"ttt" forKey:#"Name"];
[myArray addObject:myDict];
NSLog(#"myArray %#",myArray);
Regardless of the bad implementation code here (and my bad English as well :p), what I'm trying to figure out is how do I clean myDict after each insertion in myArray.
Using [myDict removeAllObjects]; after each addObject is giving me an empty array at the end.
Thanks!
Thanks for all your answers, with [myArray addObject:[[myDict copy] autorelease]]; the code works just fine.
The problem here is you're adding the exact same mutable dictionary to the array over and over. So your array is simply a collection of references to the exact same object. Therefore it shouldn't be surprising that every object in your array is identical to the last one.
In order to do what you want, you have two options. The first is to copy your mutable dictionary each time and add that copy to the array. You can do this by using
[myArray addObject:[[myDict copy] autorelease]];
The other solution is to use separate dictionaries to begin with. Instead of mutating a mutable dictionary each time, you could construct a brand new NSDictionary:
[myArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:100], #"Record",
#"www", #"Name",
nil]];
[myArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:80], #"Record",
#"eee", #"Name",
nil]];
I think you need to copy the dictionary before you add it to the array...
[myArray addObject:[[myDict copy] autorelease]];
Then you can use [myDict removeAllObjects] as normal.
NSMutableDictionary * as * suggests is a pointer to the object of dictionary.
So when you add it to an array it is still the same object - pointed by myDict as well as by the element in the array.
Therefore, I suggest you create new dictionary after each add:
NSMutableDictionary *myDict = [[NSMutableDictionary alloc] init];
NSMutableArray *myArray = [[NSMutableArray alloc] init];
[myDict setObject:[NSNumber numberWithInt:100] forKey:#"Record"];
[myDict setObject:#"www" forKey:#"Name"];
[myArray addObject:myDict];
myDict = [[NSMutableDictionary alloc] init];
[myDict setObject:[NSNumber numberWithInt:80] forKey:#"Record"];
[myDict setObject:#"eee" forKey:#"Name"];
[myArray addObject:myDict];
I assume you are using ARC in the code above.
Although, the more Objective-C'ish way would be to do:
[myArray addObject: [NSDictionary dictionaryForObjectsAndKeys: [NSNumber numberWithInt:80], #"Record", #"www", #"Name"]];
Can each index of array hold the NSDictionary?
Thank You.
Yes, the value of an NSArray can resolve to an object identifier for an NSDictionary. However, the array doesn't "hold" the NSDictionary, nor can the index of an NSArray be an NSDictionary. An index from an Array is always an integer value.
An NSArray can hold any type of object, so yes, putting an NSDictionary in an NSArray works just fine.
You sure can, here is an example:
NSDictionary *dict1 = [[NSDictionary alloc] initWithObjectsAndKeys:
#"value1", #"key1", #"value2", #"key2", nil];
NSDictionary *dict2 = [[NSDictionary alloc] initWithObjectsAndKeys:
#"Billy", #"Goat", #"Rover", #"Dog", nil];
NSArray *arrayWithDictionaries = [[NSArray alloc] initWithObjects:
dict1, dict2, nil];
[dict1 release];
[dict2 release];
I'm looking for a method of turning a NSMutableArray into a string. Is there anything on a par with this Ruby array method?
>> array1 = [1, 2, 3]
>> array1.join(',')
=> "1,2,3"
Cheers!
NSArray *array1 = [NSArray arrayWithObjects:#"1", #"2", #"3", nil];
NSString *joinedString = [array1 componentsJoinedByString:#","];
componentsJoinedByString: will join the components in the array by the specified string and return a string representation of the array.
The method you are looking for is componentsJoinedByString.
NSArray *a = [NSArray arrayWithObjects:#"1", #"2", #"3", nil];//returns a pointer to NSArray
NSString *b = [a componentsJoinedByString:#","];//returns a pointer to NSString
NSLog(#"%#", b); // Will output 1,2,3
NSArray class reference:
NSArray *pathArray = [NSArray arrayWithObjects:#"here",
#"be", #"dragons", nil];
NSLog(#"%#",
[pathArray componentsJoinedByString:#" "]);