I have an NSArray full of NSDictionary objects. Each dictionary object has a key:value:
color: red
Amongst other keys... color could be one of many colors that are user generated.
I would like to be able to extract each color and count how many I have of each, so:
red: 3 dictionaries
yellow: 4 dictionaries
etc... How exactly would this be possible? Just iterate through the dictionaries until you find a unique color, then search the array using NSPredicate again for that color, rinse, repeat?
You could just maintain a dictionary mapping color to the number of dictionaries with that color. For example:
NSMutableDictionary *counts = [NSMutableDictionary dictionary];
for (NSDictionary *dict in myArray) {
NSString *color = [dict objectForKey:#"color"];
NSNumber *val = [counts objectForKey:color];
val = [NSNumber numberWithUnsignedInteger:1+[val unsignedIntegerValue]];
[counts setObject:val forKey:color];
}
Related
I have an array NSMutableArray. It contained objects from the XML file - #"time". I need to move all records from NSMutableArray with key #"time" to NSArray. This is in order that would be based on the date on the calendar is highlighted markers. How can I implement it?
If I write the code:
NSDictionary *nItem = [rasp objectAtIndex:0]; //here instead of 0, you should put the number of elements in the array, but I do not get ((
NSArray *data = [NSArray arrayWithObjects:[nItem objectForKey:#"time"], nil];
NSMutableArray is allocated only a first date from the array, index 0.
You could do something like this.
NSMutableArray* mutable = [NSMutableArray arrayWithCapacity:0];
for(NSDictionary* nItem in rasp)
{
[mutable addObject:[nItem objectForKey:#"time"]];
}
NSArray *data = [NSArray arrayWithArray:mutable];
Hope it helps!
i have searched a lot but not able two sort my array a/c to requirement
i used this code:
[array1 sortArrayUsingSelector:#selector(caseInsensitiveCompare:) withPairedMutableArrays:arrForName, arrForAddress, nil];
thanks
On the NSArray Class Reference there isn't a - sortArrayUsingSelector:withPairedMutableArrays: method. Neither on the NSMutableArray Class Reference. If you want, you can use other methods like the NSMutableArray sortUsingSelector: method.
Put the two arrays into a dictionary as keys and values
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:arrForAddress forKeys:arrForName];
// Sort the first array
NSArray *sortedFirstArray = [[dictionary allKeys] sortedArrayUsingSelector:#selector(compare:)];
// Sort the second array based on the sorted first array
arrForAddress=[[NSMutableArray alloc]init ];
NSArray *sortedSecondArray = [dictionary objectsForKeys:sortedFirstArray notFoundMarker:[NSNull null]];
// arrForAddress = [dictionary objectsForKeys:sortedFirstArray notFoundMarker:[NSNull null]];
[arrForAddress addObjectsFromArray:sortedSecondArray];
NSLog(#"arrangesort......%#",arrForAddress);
I have looked high and low for sample Plist display code and can't seem to find any that does what I want to do. I know it can't be too hard, but I am new to Plists.
I have the following Array in a Plist and I can't seem to get the right command to display the information. Am I doing something wrong in my Plist setup? It compiles fine but no Data gets to the iPhone Simulator.
What would be the normal way of displaying the info?
I have a condition set:
If condition one
then display "+0+1"
else
display "+0+2"
*What I am hoping it to display is:
* "this is one"
* "this is two"
* "this is three"
* else display
* "555-555-1234"
* "555-555-0000"
* "555-555-5555"
MY PLIST IS AS FOLLOWS
*
Key Type Value
*Root Dictionary (2 items)
*+0+1 Array (3 items)
*Item 0 String "this is one"
*Item 1 String "this is two"
*Item 2 String "this is three"
*+0+2 Array (3 items)
*Item 0 String "555-555-1234"
*Item 1 String "555-555-0000"
*Item 2 String "555-555-5555"
*
// read property list into memory as an NSData object
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
NSString *errorDesc = nil;
NSPropertyListFormat format;
// convert static property list into dictionary object
NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc];
// assign values
self.item1 = [temp objectForKey:#"name1"];
self.item2 = [NSMutableArray arrayWithArray:[temp objectForKey:#"name2"]];
// display values
bigCheese.text = disp1;
numberZero.text = [disp2 objectAtIndex:0];
numberOne.text = [disp2 objectAtIndex:1];
numberTwo.text = [disp2 objectAtIndex:2];
Once you get a dictionary get all values for that dictionary. In your case, get all values from the temp dictionary. It should return you an array containing two arrays.
NSArray *valueArray = [temp allValues];
Now you have two arrays and if you want to access "555-555-5555", do like this:
[[valuesArray objectAtIndex:1] objectAtIndex:2];
Or if you are sure that "name1" and "name2" are the keys of your dictionary you can access it this way:
[[temp objectForKey:#"name1"] objectAtIndex:2];
Once you get this info then it is up to you to decide how you want to display it. I suggest you to DEBUG and see if the dictionary and array objects are created properly first. Not sure what are those disp1 and disp2 in the sample code you posted..
Try using:
NSString *file = [[NSBundle mainBundle] pathForResource:#"Data" ofType:#"plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:file];
NSArray *array = [dict objectForKey:#"Array"]; NSLog(#"%#", array);
It's simple, so less chances to do something wrong.
I have an NSArray full of NSDictionary objects, and each NSDictionary has a unique ID inside. I want to do lookups of particular dictionaries based on the ID, and get all the information for that dictionary in my own dictionary.
myArray contains:
[index 0] myDictionary object
name = apple,
weight = 1 pound,
number = 294,
[index 1] myDictionary object
name = pear,
weight = .5 pound,
number = 149,
[index 3] myDictionary object (etc...)
I want to get the name and weight for the second dictionary object (I won't know the index of the object... if there were only two dicts, I could just make a dictionary from [myArray objectAtIndex:1])
So, say I know the number 149. How would I be able to get the second myDictionary object out of myArray into a new NSDictionary?
As an alternative to Jacob's answer, you could also just ask the dictionary to find the object:
NSPredicate *finder = [NSPredicate predicateWithFormat:#"number = 149"];
NSDictionary *targetDictionary = [[array filteredArrayUsingPredicate:finder] lastObject];
You'd need to iterate through every NSDictionary object in your NSArray:
- (NSDictionary *) findDictByNumber:(NSInteger) num {
for(NSDictionary *dict in myArray) {
if([[dict objectForKey:#"number"] intValue] == num)
return [NSDictionary dictionaryWithObjectsAndKeys:[dict objectForKey:#"weight"], #"weight", [dict objectForKey:#"name"], #"name", nil];
}
return nil;
}
for(NSString *s in mainarr)
{
NSString newseparator = #"=";
NSArray *subarray = [s componentsSeparatedByString : newseparator];
//Copying the elements of array into key and object string variables
NSString *key = [subarray objectAtIndex:0];
NSLog(#"%#",key);
NSString *class_name= [subarray objectAtIndex:1];
NSLog(#"%#",class_name);
//Putting the key and objects values into hashtable
NSDictionary *dict= [NSDictionary dictinaryWithObject:#"class_name" forKey:#"key"];
}
Hello.. in the above code i ve to parse the elements of array in a for loop, and then have to put the substring key and class_name into a hashtable. how to put a value of those string variables into hashtable.
in the code above i guess the variables class_name and key are put into hashtable not the value. i suppose its a wrong method. wat can be done to achieve the solution?
(1), You should write
NSString* newseparator = #"=";
although directly using
NSArray *subarray = [s componentsSeparatedByString:#"="];
is much better (or make newseparator a global constant).
(2), Your last statement,
NSMutableDictionary = [NSDictionary dictinaryWithObject:#"class_name" forKey:#"key"];
is invalid because (a) NSMutableDictionary is a type; (b) you are creating a dictionary, not a mutable dictionary; (c) you are creating it every time, and overwriting the previous ones; (d) you are creating the dictionary with the constant values #"class_name" and keys #"key", which does not corresponds to the actual variables class_name and key.
To add the key-value pairs into 1 hash table, you should create the mutable dictionary at the beginning
NSMutableDictionary* dict = [NSMutableDictionary dictionary];
and then in the loop, use -setObject:forKey: to add it into the dictionary:
[dict setObject:class_name forKey:key];
To conclude, you should modify the code as
NSMutableDictionary* dict = [NSMutableDictionary dictionary];
for(NSString *s in mainarr) {
NSArray *subarray = [s componentsSeparatedByString:#"="];
// Get the elements of array into key and object string variables
NSString *key = [subarray objectAtIndex:0];
NSLog(#"%#",key);
NSString *class_name= [subarray objectAtIndex:1];
NSLog(#"%#",class_name);
//Putting the key and objects values into hashtable
[dict setObject:class_name forKey:key];
}
return dict;