1

C ++から来て、ここに私の質問があります:

私はこのタイプのオブジェクトを作成しました:

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

(各オブジェクトに@property int x;@property int y;を付けます。。)

次のように配列に格納しました:

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

今私はタイプの単一のオブジェクトを持っています:

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

@property int x;@property int y;..もあります

同様の値の配列オブジェクトが見つかるまで、その値を配列内のオブジェクトの値と比較したいと思います。しかし、Obj-Cでそれを行う方法がわかりません。(C ++では、vector v;withv.size();v[x];..etc ...を使用するだけだと思います。)

これが私が探しているものです..:)

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

助けてくれてありがとう!

4

6 に答える 6

2

まあ、配列で高速列挙を使用できます。このようなもの:

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;
    }
}
于 2012-08-09T14:25:54.203 に答える
1

別のもの:

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];
}
于 2012-08-09T14:35:40.260 に答える
0

与えられた構造を使用するだけです。ただし、それを行うよりスマートな方法があります:)

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++;
    }
}
于 2012-08-09T14:31:00.360 に答える
0

for-in ループはどうですか?

for (Size *item in array) {
   // compare 'item' to myObject
   if (/* equal condition here */) break;
}
于 2012-08-09T14:23:49.733 に答える
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;
        }
}
于 2012-08-09T14:30:11.723 に答える
0

次のようなことを試してください:

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;
  }
}
于 2012-08-09T14:25:39.093 に答える