I need to find which array a certain imageView is in, then when found do stuff with the array. Here is my first approach:
BOOL pieceMatchInLeft = NO;
BOOL pieceMatchInRight = NO;
BOOL pieceMatchInNotMoved = NO;
for (UIImageView *pieceMatch in firstWordMoveLeftArrayCopy)
{
pieceMatchInLeft = piece == pieceMatch;
if (pieceMatchInLeft) break;
}
for (UIImageView *pieceMatch in firstWordMoveRightArrayCopy)
{
pieceMatchInRight = piece == pieceMatch;
if (pieceMatchInRight) break;
}
for (UIImageView *pieceMatch in firstWordLettersNotMovedArray)
{
pieceMatchInNotMoved = piece == pieceMatch;
if (pieceMatchInNotMoved) break;
}
if (pieceMatchInNotMoved)
{
NSLog(@"Piece is in Not Moved.");
}
if (pieceMatchInRight)
{
NSLog(@"Piece is in Right.");
}
if (pieceMatchInLeft)
{
NSLog(@"Piece is in Left.");
}
I’m sure there is a much better way to do what I’m trying to do here. Doing it this way, I’m going to have to apply the exact same logic to all three if statements, rather than just the array that contains the “piece” (UIImageView)
Thanks guys!
You can use the containsObject function, which would still require three if statements but would eliminate the for loops. If you use
else ifit only evaluates the if’s till it enters one. Which helps.So it would end up looking like:
Another option is create a custom object or subclass UIImageView that contains a enum of where it is at.