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.

1
  • You are adding dictionary as an object to array, watch my answer.. Commented Jul 9, 2012 at 6:25

1 Answer 1

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)
}
Sign up to request clarification or add additional context in comments.

7 Comments

its value in the debugger is all (null)
My bad, i have given key value as value. Now i have edited, keep the key value as index
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.
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
I was able to display it. But I need to compare its indexes the value in dictionary to a integer or string.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.