Reputation: 171
I've been trying to get these results for a while now.. I can't seem to figure it out. Anyone know how to go about doing this?
I'm trying to compare two objects to each other from the beginning of my array, to the end, in that sequence.
Tilo's Solution:
for (int i=1; i<[tempRightArray count]; i++) {
UIImageView* letterA = [tempRightArray objectAtIndex:i-1];
UIImageView* letterB = [tempRightArray objectAtIndex:i];
NSLog(@"LetterA: %@",letterA);
NSLog(@"LetterB: %@",letterB);
//Distance between right side of Touched piece and Left side of new piece == Touch on Right
CGPoint midPointRightSidePiece = CGPointMake(CGRectGetMaxX(letterA.frame), CGRectGetMidY(letterA.frame));
CGPoint midPointLeftSidepiece = CGPointMake(CGRectGetMinX(letterB.frame), CGRectGetMidY(letterB.frame));
CGFloat distance = DistanceBetweenTwoPoints(midPointLeftSidepiece, midPointRightSidePiece);
NSLog(@"Distance: %f",distance);
}
Updated with Pauls block solution:
[tempRightArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if (idx > 0) {
UIImageView *letterB = (UIImageView*)obj;
id obj2 = [tempRightArray objectAtIndex:--idx]; // idx is the index of obj again given to you by the block args
UIImageView *letterA = (UIImageView*)obj2;
NSLog(@"LetterA: %@",letterA);
NSLog(@"LetterB: %@",letterB);
//Distance between right side of Touched piece and Left side of new piece == Touch on Right
CGPoint midPointRightSidePiece = CGPointMake(CGRectGetMaxX(letterA.frame), CGRectGetMidY(letterA.frame));
CGPoint midPointLeftSidepiece = CGPointMake(CGRectGetMinX(letterB.frame), CGRectGetMidY(letterB.frame));
CGFloat distance = DistanceBetweenTwoPoints(midPointLeftSidepiece, midPointRightSidePiece);
NSLog(@"Distance: %f",distance);
}
}];
Upvotes: 0
Views: 284
Reputation: 16861
Don't forget that Objective-C is a superset of C. Anything you can do in C, you can still do in Obj-C.
You probably weren't aware of the comma operator. You can execute multiple operations in each clause of a for(;;)
loop.
NSUInteger limit, i, j;
for(limit = [array count], i = 0, j = 1; j <= limit; i++, j++)
{
// i and j will increment until j hits the limit.
// do whatever you want with [array objectAtIndex:i] and [array objectAtIndex:j] in here.
}
Upvotes: 0
Reputation: 38728
How about something like this?
NSArray *array = [NSArray arrayWithObjects:@"a", @"b", @"b", @"c", @"d", nil];
[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if (idx > 0) {
// obj = the current element in the array. Given to you by the block args
id obj2 = [array objectAtIndex:--idx]; // idx is the index of obj again given to you by the block args
// Do whatever comparison you want between obj and obj2
// ...
}
}];
Don't be scared by the syntax its pretty simple. The current object is obj
and the index of that object in the array is idx
.
Upvotes: 2
Reputation: 14169
for (int i=1; i<[myArray count]; i++) {
id obj1 = [myArray objectAtIndex:i-1];
id obj2 = [myArray objectAtIndex:i];
[self compare:obj1 to:obj2];
}
Upvotes: 2
Reputation: 18512
for (i = 0; i < array_count - 1; i++) {
x = array[i];
y = array[i + 1];
/* do something with x and y */
}
Where array
is the array, array_count
is the length of array
, x
and y
are declared as the same type as the elements stored in array
. Is this what you're asking for?
Upvotes: 0
Reputation: 5095
Start the for loop with your index at one. Stop when the index is greater than or equal to array count. Compare item at index and at index - 1;
Upvotes: 0