JIGAR
JIGAR

Reputation: 302

Adding arrays with key to dictionary in ios

I am a newbie to iOS and working on an application. In this I am having an array with titles contains(a,b,c,....) as values and in another array I am having values containing name of animals as string,Now I want to store this values as key values in a dictionary,means name of the animals starts with "a" should be added like this,

 NSDictionary *animals;
    NSArray *animalSectionTitles;
    //Edit by me
    NSMutableDictionary *anim;
    NSArray *animTitles;
    NSMutableArray *commmonary;
    NSMutableArray *myary;


}

@end

@implementation AnimalTableTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    anim = [[NSMutableDictionary alloc]init];
   // animals = @[@"Bear", @"Black Swan", @"Buffalo", @"Camel", @"Cockatoo", @"Dog", @"Donkey", @"Emu", @"Giraffe", @"Greater Rhea", @"Hippopotamus", @"Horse", @"Koala", @"Lion", @"Llama", @"Manatus", @"Meerkat", @"Panda", @"Peacock", @"Pig", @"Platypus", @"Polar Bear", @"Rhinoceros", @"Seagull", @"Tasmania Devil", @"Whale", @"Whale Shark", @"Wombat"];
    animals = @{
                @"A" :@[@"Affrican cat", @"Assian cat", @"Alsesian fox"],
                @"B" : @[@"Bear", @"Black Swan", @"Buffalo"],
                @"C" : @[@"Camel", @"Cockatoo"],
                @"D" : @[@"Dog", @"Donkey"],
                @"E" : @[@"Emu"],
                @"G" : @[@"Giraffe", @"Greater Rhea"],
                @"H" : @[@"Hippopotamus", @"Horse"],
                @"K" : @[@"Koala"],
                @"L" : @[@"Lion", @"Llama"],
                @"M" : @[@"Manatus", @"Meerkat"],
                @"P" : @[@"Panda", @"Peacock", @"Pig", @"Platypus", @"Polar Bear"],
                @"R" : @[@"Rhinoceros"],
                @"S" : @[@"Seagull"],
                @"T" : @[@"Tasmania Devil"],
                @"W" : @[@"Whale", @"Whale Shark", @"Wombat"]};


    commmonary = [NSMutableArray arrayWithObjects:@"Bear", @"Buffalo",@"Camel", @"Cockatoo",@"Dog",@"Donkey",@"Emu",@"Giraffe",@"Hippopotamus", @"Horse",@"Koala",@"Lion",@"Llama",@"Lion", @"Llama",@"Manatus",@"Meerkat",@"Panda",@"Peacock",@"Pig", @"Platypus",@"Rhinoceros",@"Seagull",@"Seagull",@"Whale",@"Wombat", nil];

    animalSectionTitles = [[animals allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

   //adding alll keys in array from dictionary

    animTitles = [animals allKeys];
    NSLog(@"===My all keys for dictionary----%@",animTitles);
    NSLog(@"===my anim common aray is---%li",commmonary.count);

//new logic..!
    for(NSString *letter in animTitles)
    {
        [anim setObject:[NSMutableArray array] forKey:letter];
    }

    for(NSString *animal in commmonary)
    {
        NSString *firstLetter = [animal substringToIndex:1];
        NSMutableArray *arr = [anim objectForKey:firstLetter];
        [arr addObject:animal];


          [anim setObject: arr forKey: firstLetter];

           NSLog(@"==my array is==%@",anim);
    }

}



//new logic..!!



- (NSString *)getImageFilename:(NSString *)animal
{
    NSString *imageFilename = [[animal lowercaseString] stringByReplacingOccurrencesOfString:@" " withString:@"_"];
    imageFilename = [imageFilename stringByAppendingString:@".jpg"];

    return imageFilename;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return [animTitles count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    NSString *sectionTitle = [animTitles objectAtIndex:section];
    NSArray *sectionAnimals = [anim objectForKey:sectionTitle];
    return [sectionAnimals count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [animalSectionTitles objectAtIndex:section];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    // Configure the cell...
    NSString *sectionTitle = [animTitles objectAtIndex:indexPath.section];
    NSArray *sectionAnimals = [anim objectForKey:sectionTitle];
    NSString *animal = [sectionAnimals objectAtIndex:indexPath.row];
    cell.textLabel.text = animal;
    cell.imageView.image = [UIImage imageNamed:[self getImageFilename:animal]];

    return cell;
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
  //  return animalSectionTitles;
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    return [animalSectionTitles indexOfObject:title];
}

Here animTitles contains a,c,d...as values. Thanks in advance.

Upvotes: 2

Views: 2471

Answers (4)

ShahiM
ShahiM

Reputation: 3268

This will work. TESTED:

anim = [[NSMutableDictionary alloc] init];

animTitles = @[@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z"];

commmonary = [NSMutableArray arrayWithObjects:@"Bear",  @"Buffalo",@"Camel", @"Cockatoo",@"Dog",@"Donkey",@"Emu",@"Giraffe",@"Hippopotamus", @"Horse",@"Koala",@"Lion",@"Llama",@"Lion", @"Llama",@"Manatus",@"Meerkat",@"Panda",@"Peacock",@"Pig", @"Platypus",@"Rhinoceros",@"Seagull",@"Seagull",@"Whale",@"Wombat", nil];

for(NSString *letter in animTitles)
{
    [anim setObject:[NSMutableArray array] forKey:letter];
}

for(NSString *animal in commmonary)
{
    NSString *firstLetter = [animal substringToIndex:1];
    NSMutableArray *arr = [anim objectForKey:firstLetter];
    [arr addObject:animal];
}

Upvotes: 1

DrobyshevAlex
DrobyshevAlex

Reputation: 145

NSArray* a = @[@"Affrican cat", @"Assian cat", @"Alsesian fox"];
NSArray* b = @[@"Bear", @"Black Swan", @"Buffalo"];

NSDictionary* dict = [NSDictionary dictionaryWithObjects:b forKeys:a];

Upvotes: -2

Maulik shah
Maulik shah

Reputation: 1704

create an NSDictionary

NSArray *keys = [NSArray arrayWithObjects:@"key1", @"key2", nil];
NSArray *objects = [NSArray arrayWithObjects:@"value1", @"value2", nil];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects 
                                                       forKeys:keys];

NSMutableDictionary *mutableDict = [dictionary mutableCopy];  // for mutable

Upvotes: 1

sourav
sourav

Reputation: 819

Try replacing your for loop as this

   for (int i=0; i<animTitles.count; i++) {

        myary = [[NSMutableArray alloc]init];
        for(int j=0;j<commmonary.count;j++){
            NSString *a = [commmonary objectAtIndex:i];
            NSString *firstLetter = [a substringToIndex:1];
            if ([firstLetter isEqualToString: [animTitles objectAtIndex:i]]) {
                //adding values to array.
                [myary addObject:a];
            }
        }
        [anim setObject: myary forKey: [commmonary objectAtindex:i]];
        NSLog(@"====my Dictionary is===%@-->",anim);
    }

Upvotes: 2

Related Questions