【问题标题】:How to compare elements in int array to a integer?如何将 int 数组中的元素与整数进行比较?
【发布时间】:2011-11-17 23:40:23
【问题描述】:

我想将包含 int 值的数组与数字进行比较

示例 [1,2,30,1,40,200,10,500]

我该怎么做?

提前感谢

我这样做了,但它说操作数无效:

if ([placeName count])
{
    for (int i =0; i < [placeName count]; i++) 
    {
        NSArray *sortedArray=[placeName sortedArrayUsingFunction:intSort context:NULL];
        self.placeName = [NSMutableArray arrayWithArray:sortedArray];

        NSMutableArray *tempArray = [sortedArray objectAtIndex:i];

        NSDecimalNumber *Distance = [tempArray objectForKey:@"distance"];

        NSMutableArray *intDistanse=[Distance intValue];
        DLog(@"Distance%d", intDistanse);

        NSMutableArray *intDistanse=[Distance intValue];
        DLog(@"Distance%d", intDistanse);

        for(int j = 0; j < [intDistanse count]; j++)
        {
             if ( intDistanse[j] < 500 ) //here its says error
             {

                DLog(@"success");

             }
        }
    }
}

【问题讨论】:

  • 只需循环遍历数组的长度,将每个元素与给定的数字进行比较...初学者的问题!
  • 是的初学者问题:)
  • 请检查我编辑的问题!谢谢
  • 你不能再声明整数i了!改变它!!!
  • 错误:二进制操作数无效

标签: iphone objective-c arrays comparison int


【解决方案1】:

什么样的数组? NSArray 还是语言数组?

最简单的方法是循环。在不知道数组类型的情况下,这是伪代码。

for(int i = 0; i < arrayCount; i++)
    if ( array[i] < 500 )
         .... got an element less than 500 ....

代码没有意义:

  • 该代码应该生成大量编译器警告;每个警告都表示应该修复的错误。另外,尝试“构建和分析”。

  • 每次循环都在对数组进行排序;浪费资源,没有意义

  • 输入为 NSMutableArray* 的所有内容都没有意义;你真的有一个数组数组吗?

  • ... 调用 objectForKey: 除了 NSDictionary 以外的任何东西都不起作用

  • intValue 返回一个 (int),而不是 NSMutableArray*

  • 如果 intDistance 是一个 (int),那么它应该只是 (intDistance

总的来说,我建议你退后一步,回顾一下Objective-C language guide 和一堆工作示例。

【讨论】:

    【解决方案2】:

    通常,您循环数组中的每个元素,检查每个元素是否小于 500。

    例如:

    int i;
    for (i=0; i < THE_SIZE_OF_THE_ARRAY; ++i)
    {
      if (array[i] < 500)
      {
        /* Do something */
      }
    }
    

    【讨论】:

      【解决方案3】:

      如果您想查看是否有至少一项符合您的条件:

      bool found = false;
      for(int i = 0; i < sizeof(example)/sizeof(example[0]); ++i)
      {
          if(example[i] < 500)
          {
              found = true;
              break;
          }
      }
      

      如果您想检查所有项目是否符合您的条件:

      bool found = true;
      for(int i = 0; i < sizeof(example)/sizeof(example[0]); ++i)
      {
          if(example[i] >= 500)
          {
              found = false;
              break;
          }
      }
      

      【讨论】:

        【解决方案4】:

        确实找到了类型转换的解决方案:

        NSMutableArray *tempArray = [sortedArray objectAtIndex:i];
                //DLog(@"sortedArray%@", sortedArray);8=
                NSNumber *DistanceNum = [tempArray objectForKey:@"distance"];
                NSLog(@"distance%@:::",DistanceNum);
                NSInteger intDistance = (int)[DistanceNum floatValue];
        
                if(intDistance<500)
                {
                    NSLog(@"h:)");
                    NSString *notifications =@"Yes";
                    [[AppHelper mDataManager] setObject:notifications forKey:@"notifications"]; 
        
                    NSLog(@"notifications:%@",notifications);
                    RemindMeViewController *object = [[RemindMeViewController alloc] initWithNibName:@"RemindMeViewController" bundle:nil];
                    //  RemindMeViewController *object=[[RemindMeViewController alloc]initWithNibName];
        
                    NSLog(@"notifications set");
                    [object scheduleNotification];
                }
        

        【讨论】:

          【解决方案5】:

          你可以很容易地做到这一点,只需实现观察者进行比较。下面只是一个实现自定义观察者比较的例子

          @implementation NSArray (KVO)
          
          - (void)addObserver:(NSObject *)observer toObjectsAtIndexes:(NSIndexSet *)indexes forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context
          {
              NSUInteger idx=[indexes firstIndex];
              while(idx!=NSNotFound)
              {
                  [[self objectAtIndex:idx] addObserver:observer
                                             forKeyPath:keyPath
                                                options:options
                                                context:context];
                  idx=[indexes indexGreaterThanIndex:idx];
              }
          }
          
          - (void)removeObserver:(NSObject *)observer fromObjectsAtIndexes:(NSIndexSet *)indexes forKeyPath:(NSString *)keyPath
          {
              NSUInteger idx=[indexes firstIndex];
              while(idx!=NSNotFound)
              {
                  [[self objectAtIndex:idx] removeObserver:observer
                                                forKeyPath:keyPath];
                  idx=[indexes indexGreaterThanIndex:idx];
              }
          }
          
          -(void)addObserver:(id)observer forKeyPath:(NSString*)keyPath options:(NSKeyValueObservingOptions)options context:(void*)context;
          {
              if([isa instanceMethodForSelector:_cmd]==[NSArray instanceMethodForSelector:_cmd])
                  NSRaiseException(NSInvalidArgumentException,self,_cmd,@"not supported for key path %@ (observer was %@)", keyPath, observer);
              else
                  [super addObserver:observer
                          forKeyPath:keyPath
                             options:options
                             context:context];
          }
          
          -(void)removeObserver:(id)observer forKeyPath:(NSString*)keyPath;
          {
              if([isa instanceMethodForSelector:_cmd]==[NSArray instanceMethodForSelector:_cmd])
                  NSRaiseException(NSInvalidArgumentException,self,_cmd,@"not supported for key path %@ (observer was %@)", keyPath, observer);   
              else
                  [super removeObserver:observer
                             forKeyPath:keyPath];
          }
          @end
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-10-23
            • 2021-06-24
            • 2018-06-28
            • 1970-01-01
            相关资源
            最近更新 更多