Luis Villavicencio
Luis Villavicencio

Reputation: 107

NSArray and "If-statement"

I'm new at developing apps. I have a NSArray with 2 images in it.

imageOneArray = [NSArray arrayWithObjects:[UIImage imageNamed:@"White.png"], [UIImage imageNamed:@"Black.png"], nil];

My question is, is there a way to make an "if statement" that looks like this:

if ([imageOneArray = [UIImage imageNamed:@"White.png"]) {
    //DO SOMETHING
};

The code above is giving me error, how can i fix it? what am i doing wrong? what do i make it work?

Any help would be greatly appreciated.

Upvotes: 0

Views: 296

Answers (5)

Pétur Ingi Egilsson
Pétur Ingi Egilsson

Reputation: 4442

- (void)doStuff{
    UIImage * const targetImage = [UIImage imageNamed:@"White"];
    for (UIImage *currentImage in someArray /*replace someArray with your own*/) {
        if ([self image:targetImage isEqualTo:currentImage]) {
            // Do stuff
            break;
        }
    }
}

- (BOOL)image:(UIImage * const)firstImage isEqualTo:(UIImage * const)secondImage {
    NSData * const firstData = UIImagePNGRepresentation(firstImage);
    NSData * const secondData = UIImagePNGRepresentation(secondImage);
    return [data1 isEqual:data2];
}

Upvotes: 1

averydev
averydev

Reputation: 5727

You've got a couple of problems in the code.

Firstly "=" is an assignment e.g.

myName = Joe

whereas "==" is comparison

1 == 1 (true) 1 == 2 (false)

Secondly, an array contains multiple objects, so you would have to go through and compare each one (or use the containsObject method other answers include). You frequently would do that with a For loop.

UIImage* myImageToCompare = [UIImage imageNamed:@"White"];

for (int i=0; i<imageOneArray.count; i++) {
if ([imageOneArray objectAtIndex:i] == myImage){
...do something
}
}

Upvotes: 3

Petro Korienev
Petro Korienev

Reputation: 4027

If you need to check whether array contains an object you should go with

if ([imageOneArray containsObject:[UIImage imageNamed:@"White.png"]]) {
    //DO SOMETHING
};

Here you should note, that array checks for containment sending -isEqual: to its objects. So, on the UIImage -isEqual: will do just pointers comparison. The above code will return YES due to image caching mechanism provided by +imageNamed:, however, in general, it should return NO. Consider following example:

NSString *path = [[NSBundle mainBundle] pathForResource:@"White" ofType:@"png"];
UIImage *image1 = [UIImage imageWithContentsOfFile:path];
UIImage *image2 = [UIImage imageWithContentsOfFile:path];
NSArray *arr = @[image1];
BOOL contains1 = [arr containsObject:image1]; // YES
BOOL contains2 = [arr containsObject:image2]; // NO

And consider the following example with strings:

NSString *string1 = [NSString stringWithString:@"abc"];
NSString *string2 = [NSString stringWithString:@"abc"];
NSArray *arr = @[string1];
BOOL contains1 = [arr containsObject:string1]; // YES
BOOL contains2 = [arr containsObject:string2]; // YES

So, you should keep in mind equality rules for objects in your array. There's brilliant article on this topic Equality on NSHipster.

If you need to check whether array is equal to another array go with

if ([imageOneArray isEqualToArray:@[[UIImage imageNamed:@"White.png"]]]) {
    //DO SOMETHING
};

Take a look at NSArray class reference to see when you can do with arrays

Upvotes: 5

Huy Nghia
Huy Nghia

Reputation: 986

You could try the following, assuming the goal was to check if the array contains an image named "White". Notice that UIImage -imageNamed: does not require the extension

if ([imageOneArray containsObject:[UIImage imageNamed:@"White"]]) {
    //DO SOMETHING
}

Upvotes: 4

Divjot Singh
Divjot Singh

Reputation: 114

You cab use containObject in the Array -containsObject: compares the objects using -isEqual:, which is usually what you want: e.g., two NSString objects containing the same UNICODE characters would be considered equal, even if they are not the same object.

Upvotes: 2

Related Questions