1

In my Array 1, which I loaded images from NSDocumentDirectory, I loaded them and add a NSMutableDictionary:

self.images = [NSMutableArray array];
for(int i = 0; i <= 8; i++) 
{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];

    NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"Images%d.png", i]]; 
    if([[NSFileManager defaultManager] fileExistsAtPath:savedImagePath]){ 
        NSMutableDictionary *container = [[NSMutableDictionary alloc] init];
        [container setObject:[UIImage imageWithContentsOfFile:savedImagePath] forKey:@"image"];
        [container setObject:[NSNumber numberWithInt:i] forKey:@"index"];
        [images addObject:container];
    } 
}   

In my other array Array 2, I loaded them from the app.

    self.images = [NSMutableArray arrayWithObjects:
                   @"01.jpg",@"02.jpg",@"03.jpg",@"04.jpg",@"05.jpg",@"06.jpg",@"07.jpg",nil];

I was able to add string to Array 2 like this:

for (int x=1; x<8;x++) {
    // add as NSString
    [images addObject:[NSString stringWithFormat:@"%d", x]];

    }

And was able to compare Array 2 like this:

   NSInteger index = carousel.currentItemIndex;
    if ([[images objectAtIndex:index] intValue] == 1){

What I wanted to do, is to do it to Array 1. I know Array 1 has been already added with NSNumber, but Im kinda new to NSMutableDictionary so I can't do it the same as Array 2.

Can it be done the same as my Array 2 or what is the other way?

Thanks for the help.

Bazinga
  • 2,456
  • 33
  • 76

1 Answers1

3

Actually Here you have the array of dictionaries, because you are adding the dictionary which has two objects with keys image and index

So, for retrieving the array of dictionaries,

just log it and see what happens

Edit 2.0

for(int i=0; i< [images count]; i++){
    NSNumber *num =[[images objectAtIndex:i] objectForKey:@"index"];

    int index = [num intValue];

    NSLog(@"%d",index)
}
Charan
  • 4,940
  • 3
  • 26
  • 43
  • its value in the debugger is all (null) – Bazinga Jul 09 '12 at 06:27
  • My bad, i have given key value as **value**. Now i have edited, keep the key value as **index** – Charan Jul 09 '12 at 06:28
  • yup, already have change it. Its index wag logged. But how to compare it like the above I mention. I kinda confused with this dictionaries. – Bazinga Jul 09 '12 at 06:29
  • You have to retireve the objects in the dictionary according to its corresponding key value and compare it inside the for loop, because you are getting the values inside it – Charan Jul 09 '12 at 06:30
  • I was able to display it. But I need to compare its indexes the value in dictionary to a integer or string. – Bazinga Jul 09 '12 at 06:32
  • Check my edit now, in the index you will get your index value from dictionary and then just compare like if (index == 1) – Charan Jul 09 '12 at 06:36
  • still not working the way I wanted it to,like the above, but thanks for the tip. my `Array 2` was accurate. I can't make it like that. – Bazinga Jul 09 '12 at 08:37