Вопрос

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]];
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top