1

Coming from C++, here's my question :

I have created objects of this type :

    Size *one = [[Size alloc] initWithX: 3 andY: 1];
    Size *two = [[Size alloc] initWithX: 4 andY: 7];
    // etc...
    Size *thirtythree = [[Size alloc] initWithX: 5 andY: 9];

( with a @property int x; & @property int y; for each object.. )

that I have stored in an array as follows :

NSArray *arrayOfSizes;

arrayOfSizes = [NSArray arrayWithObjects:one,two,three,four,five,six,
                seven,eight,nine,ten,eleven,twelve,thirteen,
                fourteen,fifteen,sixteen,seventeen,eighteen,
                nineteen,twenty,twentyone,twentytwo,
                twentythree,twentyfour,twentyfive,twentysix,
                twentyseven,twentyeight,twentynine,thirty,
                thirtyone,thirtytwo,thirtythree nil];

now I have a single object of type :

Myobject *myObject = [[Myobject alloc] initWithX: 5 andY: 3];

that also has a @property int x; & @property int y; ...

and I want to compare its values to the values of the objects found in the array, until I find an array object of similar values.. But I don't know how to do that in Obj-C. (in c++ I would simply use a vector v; with v.size(); and v[x]; ..etc... I suppose..)

here's what I'm looking for.. :)

while( !wholeOfArrayOfSizesChecked && !found)
{
    if ( // x & y of object in array is equal to x & y of myObject )
    {
        found = YES;
    }
    else if( // whole of array checked)
    {
       wholeOfArrayOfSizesChecked = YES;
    }
    else
    { 
      //move on to the next object of the array..
    }
}

Thanks in advance for any help!

0

6 Answers 6

2

Well, you could just use fast enumeration on the array. Something like this:

Myobject *myObject = [[Myobject alloc] initWithX: 5 andY: 3];

for (Size *s in arrayOfSizes)
{
    if (s.x == myObject.x && s.y == myObject.y)
    {
        // Found one
        // Do something useful...
        break;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

according to documentation, the only problem with that is that once you break from the loop, the pointer stays pointing at the last object that was being checked. So next time I go into that for loop it won't start checking from the very top if I understand correctly...
@SproutCoder No that is not true. Every time you run this loop the array will be enumerated from the first element. If you had declared your s before the loop, then it would have the last value before the break (in my example s is scoped inside the loop). But even then, the next time you'd go inside the loop the array would be enumerated again from its start. I hope that this makes sense.
1

Another one:

NSUInteger index = [arrayOfSizes indexOfObjectPassingTest:
    ^BOOL(Size *s, NSUInteger idx, BOOL *stop)
    {
        return (s.x == myObject.x) && (s.y == myObject.y);
    }
];

if (index != NSNotFound) {
    id object = [arrayOfSizes objectAtIndex:index];
}

Comments

0

Just to use your given structure. There are smarter ways of doing it though :)

wholeOfArrayOfSizesChecked = NO; 
int currObj = 0  
while( !wholeOfArrayOfSizesChecked && !found)
{
    Size *current = (Size *)[arrayOfSizes objectAtIndex:i];
    if (myObject.x == current.x && myObject.y == current.y)
    {
        found = YES;
    }
    else if(currObj == [arrayOfSizes count] -1 )
    {
       wholeOfArrayOfSizesChecked = YES;
    }
    else
    { 
      currObj++;
    }
}

2 Comments

I'm using beyerss for loop but his [arrayOfSizes size] gives an error.. Could I simply replace that with your [arrayOfSizes count] -1 and use the following : for (int i = 0; i < [arrayOfSizes count]-1; i++) ?
@SproutCoder, yes, you can do that.
0

Try something like this:

for (int i = 0; i < [arrayOfSizes size]; i++)
{
  Size *current = (Size *)[arrayOfSizes objectAtIndex:i];
  if (myObject.x == current.x && myObject.y == current.y)
  {
    // found
    break;
  }
}

2 Comments

hmm... I get a no visible NSArray declares the selector 'size'
replace size with count. Just a little work on your own and you would have seen that.
0

How'bout a for-in loop?

for (Size *item in array) {
   // compare 'item' to myObject
   if (/* equal condition here */) break;
}

Comments

0
-(BOOL) isSize:(Size*)size equalToMyObject:(MyObject*)object{
    return (size.x == object.x) && (size.y == object.y);
}

//In some method where you are checking it:
for (Size* size in arrayOfSizes){
        if ([self isSize:size equalToMyObject:myObject]){
            //You found it! They're equal!
            break;
        }
}

1 Comment

& should be && in the comparison function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.