如何将int数组中的元素与整数进行比较?

时间:2011-09-15 15:06:29

标签: iphone objective-c arrays comparison int

我想将包含int值的数组与数字

进行比较

示例[1,2,30,1,40,200,10,500]< 500表示如果数组包含小于500的元素。

我该怎么做?

提前感谢

我这样做了它说无效的opernd:

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");

             }
        }
    }
}

5 个答案:

答案 0 :(得分:3)

什么样的阵列? 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&lt; 500)

总的来说,我建议您退后一步,查看Objective-C language guide和一些有用的示例。

答案 1 :(得分:1)

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

例如:

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

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

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

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

答案 4 :(得分:-2)

只需实现观察者进行比较,即可轻松完成此任务。以下只是实现自定义观察器进行比较的示例

@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
相关问题