如何将 int 数组中的元素与整数进行比较?
我想将包含 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
什么样的数组? NSArray 还是语言数组?
最简单的方法是循环。在不知道数组类型的情况下,这是伪代码。
该代码实际上没有意义:
该代码应该生成大量编译器警告;每个警告都表明应该修复一个错误。另外,尝试“构建和分析”。
每次循环都对数组进行排序;浪费资源并且没有意义
所有被输入为 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.
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.
通常,您循环数组中的每个元素,检查每个元素是否小于 500。
例如:
Typically, you loop over each element in the array, checking if each element is less than 500.
For example:
如果您想查看是否至少有一项符合您的条件:
如果您想检查所有项目是否符合您的条件:
If you want to see whether there is at least one item that matches your condition:
And if you want to check whether all items match your condition:
是否找到了类型转换的解决方案:
Did find a solution to it type casting:
您只需实现观察者进行比较就可以非常轻松地做到这一点。以下只是实现自定义观察者进行比较的示例
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