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

发布于 2024-12-04 21:52:14 字数 970 浏览 5 评论 0原文

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

示例进行比较 [1,2,30,1,40,200,10,500] < 500 表示数组包含的元素少于 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 want to compare an array containing int values to a number

example [1,2,30,1,40,200,10,500] < 500 meaning if array contains elements less than 500.

How can i do dis?

Thank alot in advance

I did this bt it say invalid 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");

             }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

说好的呢 2024-12-11 21:52:14

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

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

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

该代码实际上没有意义:

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

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

  • 所有被输入为 NSMutableArray* 的东西都是没有意义的;你真的有一个由数组组成的数组吗?

  • ... 在除 NSDictionary 之外的任何内容上调用 objectForKey: 都不起作用

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

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

总的来说,我建议您退一步并查看 Objective-C 语言指南 和一堆工作示例。

What kind of an array? NSArray or language array?

Easiest way is a loop. Without knowing the type of the array, this is pseudo code.

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

The code doesn't really make sense:

  • that code should be generating a ton of compiler warnings; every warning indicates a bug that should be fixed. Also, try "Build and Analyze".

  • you are sorting an array every time through the loop; waste of resources and doesn't make sense

  • everything being typed as NSMutableArray* doesn't make sense; do you really have an array of arrays of arrays?

  • ... calling objectForKey: on anything but an NSDictionary doesn't work

  • intValue returns an (int), not an NSMutableArray*

  • if intDistance is an (int), then it should just be ( intDistance < 500 )

Overall, I would suggest you step back and review the Objective-C language guide and a bunch of working examples.

幼儿园老大 2024-12-11 21:52:14

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

例如:

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

Typically, you loop over each element in the array, checking if each element is less than 500.

For example:

int i;
for (i=0; i < THE_SIZE_OF_THE_ARRAY; ++i)
{
  if (array[i] < 500)
  {
    /* Do something */
  }
}
听风吹 2024-12-11 21:52:14

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

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

If you want to see whether there is at least one item that matches your condition:

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

And if you want to check whether all items match your condition:

bool found = true;
for(int i = 0; i < sizeof(example)/sizeof(example[0]); ++i)
{
    if(example[i] >= 500)
    {
        found = false;
        break;
    }
}
病毒体 2024-12-11 21:52:14

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

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

Did find a solution to it type casting:

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];
        }
魔法少女 2024-12-11 21:52:14

您只需实现观察者进行比较就可以非常轻松地做到这一点。以下只是实现自定义观察者进行比较的示例

@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

You can do this very easily by just implementing the observer to compare. Following is just an example to implement the custom observer to compare

@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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文