简体   繁体   中英

How do I parse through an array of objects in Objective-C?

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!

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;
    }
}

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];
}

How'bout a for-in loop?

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

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;
  }
}
-(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;
        }
}

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++;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM