Comparing with NSMutableArray

2019-07-30 14:57发布

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条回答
我欲成王,谁敢阻挡
2楼-- · 2019-07-30 15:18

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)
}
查看更多
登录 后发表回答