I have an iPad application and I have an object with five attributes of type bool. I have an UIImageView where I have to put an image into depending which of these attributes is true. This is my source code which works fine:
if (myObject.attribute1) {
myCell.imageView.image = [UIImage imageNamed:@"imageForAttribute1.png"];
} else if (myObject.attribute2) {
myCell.imageView.image = [UIImage imageNamed:@"imageForAttribute2.png"];
} else if (myObject.attribute3) {
myCell.imageView.image = [UIImage imageNamed:@"imageForAttribute3.png"];
} else if (myObject.attribute4) {
myCell.imageView.image = [UIImage imageNamed:@"imageForAttribute4.png"];
} else if (myObject.attribute5) {
myCell.imageView.image = [UIImage imageNamed:@"imageForAttribute5.png"];
}
For me, it looks not so nice. Isn’t there a nicer way to solve this “problem”?
Best Regards, Tim.
Are the attributes all truly independent? Based on the logic you’re showing, it seems that you’re expecting that only one attribute will be set at a time.
If that’s the case, maybe the attributes could be combined into a single enum value, with possible values of 1-5, then you could just use that number to select the image: