【问题标题】:How do I parse through an array of objects in Objective-C?如何解析 Objective-C 中的对象数组?
【发布时间】:2012-08-06 19:09:02
【问题描述】:

来自 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;v.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..
    }
}

提前感谢您的帮助!

【问题讨论】:

    标签: objective-c ios arrays while-loop


    【解决方案1】:

    for-in 循环怎么样?

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

    【讨论】:

      【解决方案2】:

      试试这样的:

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

      【讨论】:

      • 嗯...我收到了no visible NSArray declares the selector 'size'
      • 用计数替换大小。只需您自己做一点工作,您就会看到。
      【解决方案3】:

      好吧,您可以在阵列上使用fast enumeration。像这样的:

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

      【讨论】:

      • 根据文档,唯一的问题是一旦你从循环中中断,指针就会一直指向最后一个被检查的对象。所以下次我进入那个 for 循环时,如果我理解正确,它就不会从最顶层开始检查......
      • @SproutCoder 不,那不是真的。每次运行此循环时,都会从第一个元素枚举数组。如果您在循环之前声明了s,那么它将具有中断之前的最后一个值(在我的示例中,s 的范围在循环内)。但即便如此,下次您进入循环时,数组将从其开始再次被枚举。我希望这是有道理的。
      【解决方案4】:
      -(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;
              }
      }
      

      【讨论】:

      • &amp; 在比较函数中应该是&amp;&amp;
      • @MartinR 感谢您指出这一点。我错过了。现在它已经修复了。
      【解决方案5】:

      只是为了使用你给定的结构。不过有更聪明的方法:)

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

      【讨论】:

      • 我正在使用 beyerss for 循环,但他的 [arrayOfSizes size] 给出了错误。我可以简单地将其替换为您的 [arrayOfSizes count] -1 并使用以下内容:for (int i = 0; i &lt; [arrayOfSizes count]-1; i++) 吗?
      • @SproutCoder,是的,你可以这样做。
      【解决方案6】:

      另一个:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-13
        相关资源
        最近更新 更多