I am new to objective c. I am interesting in having something like this:
- for each key I want to store multiple values like:
2 holds a,b,c
3 holds d,e,f
When pressing 2 3 or 2 3 3, I want to have at output all the combinations from these 6 values. Should I use a NSMutableDictionary for this? I need some advices!
You can store arrays in dictionaries. For example
NSDictionary *mapping = #{#"2": #[#"a", #"b", #"c"]};
and you could for each key press add the objects from the array in the dictionary to an intermediate array
NSMutableArray *values = [NSMutableArray array];
...
// For each time a key is pressed
[values addObjectsFromArray:#[mapping[keyPressed]]];
...
When you want to display the output you calculate all combinations for all values in the values array.
For store multiple value of single key, you need to add array as value of dictionary key, such like,
NSArray *temArray1 = ...// which has value a,b,c
NSArray *temArray2 = ...// which has value d,e,f
Add this array as value of specific key, such like
NSMutableDictionary *temDic = [[NSMutableDictionary alloc] init];
[temDic setValue:temArray1 forKey#"2"];
[temDic setValue:temArray1 forKey#"3"];
NSLog(#"%#", temDic)
Above code describe simple logic as per your requirement change it as you need.
Please Try this..
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
NSArray *aryFlashCardRed=[[NSArray alloc]initWithObjects:#"f1",#"f2",#"f3", nil];
NSArray *aryFlashCardYellow=[[NSArray alloc]initWithObjects:#"f4",#"f5",#"f6", nil];
NSArray *aryFlashCardGreen=[[NSArray alloc]initWithObjects:#"f7",#"f8",#"f9", nil];
NSArray *aryScore=[[NSArray alloc]initWithObjects:#"10",#"20",#"30", nil];
[dictionary setObject:aryFlashCardRed forKey:#"red"];
[dictionary setObject:aryFlashCardYellow forKey:#"yellow"];
[dictionary setObject:aryFlashCardGreen forKey:#"green"];
[dictionary setObject:aryScore forKey:#"score"];
Display dictionary like this
{
green = (
f7,
f8,
f9
);
red = (
f1,
f2,
f3
);
score = (
10,
20,
30
);
yellow = (
f4,
f5,
f6
);
}
Objective-c has 3 types of collections: NSArray, NSSet, NSDictionary (and their mutable equivalents). All this collections can store only objects. This collections uses for different cases, so try to find case which is appropriate to You and use appropriate collection.
P.S. My first wish was to write RTFM
Try like below :-
NSDictionary *yourDict=[NSDictionary dictionaryWithObjects:
[NSArray arrayWithObjects:#"a", #"b", #"c",nil]
forKeys:[NSArray arrayWithObjects:#"2",nil]];
Related
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.
What is the difference between these two NSArray outputs using NSLog?
//Use to get all values from within Dictionary
NSArray * values = [self.dict allValues];
//Title
self.namesArray = [values valueForKey:#"Imade"];
gives the following which is broken up into the KEY values they are stored in:
Names : (
(
tttt,
tgrtg,
trgrtgrtgrtgrtg
),
(
fcxczxc,
zcxzc,
asad
),
(
sdedw,
frfefr
)
)
and
self.colorArray= [[NSArray alloc] initWithObjects: #"Red", #"Yellow", #"Green",
#"Blue", #"Purpole", nil];
gives
Names : (
Red,
Yellow,
Green,
Blue,
Purpole
)
Now I can populate a uiTableViewCell with the colored NSArray however I cannot with the Names NSArray. Why is this and how do I fix it? Do I need to break the namesArray down further and how do I do this? Im sorry if this is obvious.
If needed
//Use to get Key Values from within Dictionary
self.sectionArray = [self.dict allKeys];
UPDATE:
It looks like the first array is an array of arrays, and the second one is an array of strings.
According to NSArray class reference there are 4 type of methods to sort array:
1- sortedArrayUsingComparator:
2- sortedArrayUsingSelector:
3- sortedArrayUsingFunction:context:
4- sortedArrayUsingDescriptors:
For first three methods it mentioned :
The new array contains references to the receiving array’s elements, not copies of them.
But for the forth method (descriptor) it mentioned:
A copy of the receiving array sorted as specified by sortDescriptors.
But following example shows like the other 3 methods, descriptor also retain original array and do not return a new copy of it:
NSString *last = #"lastName";
NSString *first = #"firstName";
NSMutableArray *array = [NSMutableArray array];
NSDictionary *dict;
NSMutableString *FN1= [NSMutableString stringWithFormat:#"Joe"];
NSMutableString *LN1= [NSMutableString stringWithFormat:#"Smith"];
NSMutableString *FN2= [NSMutableString stringWithFormat:#"Robert"];
NSMutableString *LN2= [NSMutableString stringWithFormat:#"Jones"];
dict = [NSDictionary dictionaryWithObjectsAndKeys: FN1, first, LN1, last, nil];
[array addObject:dict];
dict = [NSDictionary dictionaryWithObjectsAndKeys: FN2, first, LN2, last, nil];
[array addObject:dict];
// array[0].first = "Joe" , array[0].last = "Smith"
// array[1].first = "Robert" , array[1].last = "Jones"
NSSortDescriptor *lastDescriptor =[[NSSortDescriptor alloc] initWithKey:last
ascending:YES
selector:#selector(localizedCaseInsensitiveCompare:)];
NSSortDescriptor *firstDescriptor =[[NSSortDescriptor alloc] initWithKey:first
ascending:YES
selector:#selector(localizedCaseInsensitiveCompare:)];
NSArray *descriptors = [NSArray arrayWithObjects:lastDescriptor, firstDescriptor, nil];
NSArray *sortedArray = [array sortedArrayUsingDescriptors:descriptors];
// array[1] == sortedArray[0] == ("Robert" , "Jones")
// comparing array entries whether they are same or not:
NSLog(#" %p , %p " , [array objectAtIndex:1] , [sortedArray objectAtIndex:0] );
// 0x10010c520 , 0x10010c520
it shows objects in both arrays are same,
"A copy of the receiving array sorted as specified by sortDescriptors" means that the array object is copied not the elements in the array. The reason the documentation uses the word "copy" is to make it clear that the returned array is not the same array instance as the receiver.
Elements in an array are never copied in Cocoa with the exception of initWithArray:copyItems:YES which will copy the first level items in the original array to the new array. Even then, this copy is done by calling copyWithZone: on the elements, so caveats apply depending on what elements are in your array.
Note that Cocoa is reference counted, so the concept of "deep copies" is not inherently built in for a reason. This is also (in part) the reason why array objects in cocoa come in two flavors (NSArray and NSMutableArray) and are usually immutable (NSArray) instead of as in other languages where there is not usually a concept of immutable and mutable arrays.
see this SO answer for how to get a "deep copy" of an NSArray.
I would like some help sorting an NSArray of NSDictionary values based on each objects ISV key.
This is the code I have so far for creating my array objects so you have a better idea of what I am trying to do.
NSArray *combinedKeysArray = [NSArray arrayWithObjects:#"HASM", #"ISL", #"ISV", nil];
valuesCombinedMutableArray = [NSMutableArray arrayWithObjects:[dict objectForKey:#"HASM"],
[dict objectForKey:#"ISL"],
[dict objectForKey:#"ISV"],
nil];
combinedDictionary = [NSDictionary dictionaryWithObjects:valuesCombinedMutableArray
forKeys:combinedKeysArray];
[unSortedrray addObject:combinedDictionary];
// how do I then sort unSortedArray by the string values in each object ISV key?
any help would be greatly appreciated.
This can solve your problem
How to sort an NSMutableArray with custom objects in it?
https://stackoverflow.com/a/805589/1294448
You can use NSSortDescriptor to sort NSArays
Then in NSArray you have a method called sortedArrayUsingDescriptors
Or NSComparisonResult ca also be helpful some time http://developer.apple.com/DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html#//apple_ref/doc/uid/20000138-BABCEEJD
you won't be able to sort unSortedArray because it will only have one element in it (ie in your last line of code you are adding a single object by addObject).
That said, you cannot sort the dictionary either.. b/c dictionaries are unsorted by definition.
you can iterate over the keys of the dictionary in a specific order though, you can sort an array containing the keys of the dictionary.
NSArray *keys = [theDictionary allKeys];
NSArray *sortedKeys = [keys sortedArrayUsingSelector:#selector(compareMethod:)];
You can use -sortedArrayUsingComparator: to sort any way you need.
[unSortedrray sortedArrayUsingComparator:^NSComparisonResult(NSDictionary *dict1, NSDictionary *dict2) {
return [[dict1 objectForKey:#"ISV"] localizedCompare:[dict2 objectForKey:#"ISV"]];
}];
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.