我如何获取 NSArray 最大对象
我有一个 NSArray ,数组是这样的数字;
NSArray *ratio = [NSArray arrayWithObjects:@"99",@"88",@"77", @"66", @"55", @"44",@"33", @"22",@"11",@"0",@"-11",@"-22",@"-33",@"-44",@"-55",@"-66",@"-77",@"-88",@"-99",@"-100",nil];
我想获取最大的物体 99 和最小的物体 -100 hiw 我可以这样做吗?
我将使用 if 条件最大的对象,如下所示;
if ([[ratio objectAtIndex:i] intValue] == 100 ) {
rd = 0;
gr = 0.5;
bl =0;
}
//我想这样做
if ([[ratio objectAtIndex:i] intValue] == biggestobject or smallestobject ) {
///.....
}
请帮助我的朋友:( - 现在解决问题,但下面还有另一个问题。
第二个问题:(
NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self" ascending: NO];
NSLog(@"%@",[ratio sortedArrayUsingDescriptors: [NSArray arrayWithObject: sortOrder]]);
我对数组进行了排序,但无法从数组中选择最大和最小的对象。我该怎么做?
NSLog屏幕如下所示;
我如何在排序数组中获取 objectforkey:0
i have a NSArray and arrays are numbers like this;
NSArray *ratio = [NSArray arrayWithObjects:@"99",@"88",@"77", @"66", @"55", @"44",@"33", @"22",@"11",@"0",@"-11",@"-22",@"-33",@"-44",@"-55",@"-66",@"-77",@"-88",@"-99",@"-100",nil];
i want to take biggest object 99 and smallest object -100 hiw can i do this ?
i will use if condition biggest object like this ;
if ([[ratio objectAtIndex:i] intValue] == 100 ) {
rd = 0;
gr = 0.5;
bl =0;
}
//i want to do this
if ([[ratio objectAtIndex:i] intValue] == biggestobject or smallestobject ) {
///.....
}
Help pls myfriends:( - Now solve problem but there is a another problem at follows.
Second one problem :(
NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self" ascending: NO];
NSLog(@"%@",[ratio sortedArrayUsingDescriptors: [NSArray arrayWithObject: sortOrder]]);
i sorted array but cannot choose biggest and smallest object from array. How can i do this?
NSLog screen is following like this; 99, 99, 99, 91, 91, 88, 88, 88, 88, 77, 77, 66, 66, 66,
how can i take objectforkey:0 at sorted array
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一种方法是对数组进行排序并获取第一个和最后一个索引。
按降序对 NSArray 进行排序
One way would be to just sort the array and take the first and last index.
Sort an NSArray in Descending Order
这会解决你的问题吗:
will this solve your issue:
编写此方法对您定义的两个字符串进行排序,请注意
NSNumericSearch
。 请参阅此处的文档。然后使用以下命令进行排序:
Write this method to sort two strings as you have defined, note the
NSNumericSearch
. See Documentation here.then sort using:
要获取值,请使用以下代码:
您需要使用:
to get the values, instead the following code:
you need to use: