My dictionary looks like this
#{#"Blue": #"Big",
#"Red": #"medium",
#"Yellow": #"small"}
I would like to know that the highest key length is 6, because Yellow is the longest key
You can try this. Suppose a is your dictionary. You can find the source here: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/KeyValueCoding/Articles/CollectionOperators.html
NSArray *array = a.allKeys;
NSNumber* maxLength= [array valueForKeyPath:#"#max.length"];
NSLog(#"Longest is %lu",maxLength.integerValue);
Related
How can I convert a known array in to string in TCL? an array might have values such as root_user_appversion 10.1.3.20 and/or I just want to take out the last values out of it which 10.1.3.20 .
You can transform the array in list:
set my_list [array get my_array]
puts "last element: [lindex $my_list [expr {[llength $my_list] -1}] ]"
After that, you can easily convert your list in string with join:
set my_string [join $my_list " "]
I think you want
join [dict values [array get the_array]]
Which takes a list of alternating key / value items, filters out the value items, and joins them into a string.
Note that values with spaces will be munged: in that case you're better off with just dict values [array get the_array].
Documentation: array, dict, join
I have 3 NSArrays with:
item: amount
A: 1
B: 2
C: 3
A: 2
E: 1
F: 6
C: 5
D: 1
F: 3
After "combining" these into one, I need:
A: 3
B: 2
C: 8
D: 1
E: 1
F: 9
Do I first combine all the arrays into one and then sum and remove the duplicates?
You could use an NSCountedSet. I'm not clear on the structure of the data in your arrays, but by assuming that your B: 2 means that you have two B's in the array, then something like this would work:
NSCountedSet *set = [NSCountedSet setWithCapacity:[array1 count]+[array2 count]+[array3 count]];
[set addObjectsFromArray:array1];
[set addObjectsFromArray:array2];
[set addObjectsFromArray:array3];
// Test it out!
NSUInteger countForC = [set countForObject:objC];
// countForC == 8
Instead of using a NSArray you could try using a NSMutableDictionary where the key is inherent in the objects structure. That will allow you to iterate through each of your arrays of letters and counts then query for the value with the key, get the value and add to the value, then continue processing.
One possibility would be to use:
Use predicates to extract like sets of data (by item) into separate arrays. See Collection predicates guide
Key Value Coding to sum the value field of each of the resulting arrays (by item). See KVO collection operators.
Pop the results in whatever structure you like (NSArray or NSDictionary).
There may be performance considerations to explore. Alternatively, iterate the array, pulling out matching items in a separate NSDictionary (keyed on item) and summing as you go.
I want the number of Spaces in OSX snow leopard.
If I use 4 spaces, I want that number 4!
I can't find a function or a class related with that.
Information about Spaces is stored in Dock preferences. You can access the preferences via NSUserDefaults to get the number of rows and columns, and the number of spaces is the product of those:
NSDictionary *dockPrefs = [[NSUserDefaults standardUserDefaults]
persistentDomainForName:#"com.apple.dock"];
int rows = [[dockPrefs objectForKey:#"workspaces-rows"] intValue];
int cols = [[dockPrefs objectForKey:#"workspaces-cols"] intValue];
int nspaces = rows * cols;
How to get a substring from NSSTring from index 0 to 99. i.e. first 100 characters
[myString substringToIndex:100]
You need to be sure that index 100 is valid, i.e. length of string is at least 100. Otherwise an exception will be thrown.
[str substringToIndex: 100]
I have 2 NSArray's that are holding values...
For example NSArray 1 has values 1 2 4 in it
and NSArray 2 has values 1 2 4 5 6 in it.
How can I write code to compare these 2 arrays to get the following information...
Count the values that are the same (so in this case 3) and count the values that are not the same (in this case 2).
I am simply populating the arrays like this:
NSString *s = #"1,2,4";
NSArray *numbers = [s componentsSeparatedByString:#","];
where *s is actually getting the text from a UITextField. If sorting mattering in comparing can you show me code to sort to make sure the user doesnt put the numbers in order?
If you are fine with sets instead of arrays, you can use NSMutableSet instead of NSArray. NSMutableSet has nice methods like intersectSet: and minusSet:
I would probably use the following method of the NSArray class:
enumerateObjectsUsingBlock.
and code the block testing for membership in the other array with the method:
indexOfObjectIdenticalTo.
If this isn't clear to you let me know.