OBJ-C: 在NSMutableArray中获取最小/最大值

10
我想要获取一个NSMutableArray的最大值和最小值,这样我就可以创建基于这些最大值和最小值的core-plot plotspace和图形。
我的代码如下:
NSMutableArray *contentArray = [NSMutableArray arrayWithCapacity:100];
NSUInteger i;
for (i=0; i<60; i++){
id x = [NSNumber numberWithFloat:i*0.05];
id y = [NSNumber numberWithFloat:1.2*rand()/(float)RAND_Max + 0.6];
[contentArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x, @"x", y, @"y", nil]];
}

self.dataForPlot = contentArray;

CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat() length:CPDecimalFromFloat()];
plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat() length:CPDecimalFromFloat()];

如果我想让图表仅跨越contentArray中指定的空间,那么在xRange和yRange分配的空白处填写什么?

5个回答

21
在这种情况下,你应该使用-[CPPlotSpace scaleToFitPlots:]。如果想进行更一般的数组值计算,请继续阅读...
这是键-值编码和关联的数组运算符的理想用例。在你的示例中,
float maxX = [[contentArray valueForKeyPath:@"@max.x"] floatValue];
float minY = [[contentArray valueForKeyPath:@"@min.x"] floatValue];

float maxY = [[contentArray valueForKeyPath:@"@max.y"] floatValue];
float minY = [[contentArray valueForKeyPath:@"@min.y"] floatValue];

数组操作符在contentArray上调用valueForKeyPath:,这会返回一个数组,该数组是通过对数组中的每个成员调用相同的valueForKeyPath:并使用数组运算符右侧的键路径来构建的(即@min@max)。原始调用随后将给定的运算符应用于生成的数组。您可以轻松地在NSArray上定义一个类别,以便为您提供最小/最大值的结构体:

typedef struct {
  float minX; 
  float minY;
  float maxX;
  float maxY;
} ArrayValueSpace;

@implementation NSArray (PlotSpaceAdditions)
- (ArrayValueSpace)psa_arrayValueSpace {
  ArrayValueSpace result;

  result.maxX = [[contentArray valueForKeyPath:@"@max.x"] floatValue];
  result.minX = [[contentArray valueForKeyPath:@"@min.x"] floatValue];

  result.maxY = [[contentArray valueForKeyPath:@"@max.y"] floatValue];
  result.minY = [[contentArray valueForKeyPath:@"@min.y"] floatValue];

  return result;
}

谢谢Barry,你的回答非常有帮助。你知道是否也有轴自动调整功能吗? - Kevin
CPAxis 类支持自动标签策略 (CPAxisLabelingPolicyAutomatic)。我认为,轴会自动跨越绘图空间,因此您只需要将标签策略设置为自动,然后告诉它在绘图空间大小更改时重新标记即可。 - Barry Wark
@BarryWark 如果我有两个或更多不同的数组要在同一图形区域中绘制,该怎么办?我如何获取所有数组中最小和最大的Y值? - Juan Andres

1

当你填写时,可以找到最大值和最小值,但只有在你的片段中使用时才有效。如果你想要更通用的东西,你应该简单地浏览列表并搜索它。

// contentArray already defined
int maxX = 0, minX = 1000, maxY = 0, minY = 1000;

for (int i = 0; i < [contentArray count]; ++1)
{
   int curX = [[[contentArray objectForKey:@"x"] objectAtIndex:i] integerValue];
   int curY = [[[contentArray objectForKey:@"y"] objectAtIndex:i] integerValue];

   if (curX < minX)
     minX = curX;
   else if (curX > maxX)
     maxX = curX;

   if (curY < minY)
     minY = curY;
   else if (curY > maxY)
     maxY = curY;
}

0
如果你有一个包含NSDictionary对象的NSMutableArray,你可以使用以下代码:
-(float)findMax:array arrayKey:obj {
    float max = [[[array objectAtIndex:0] objectForKey:obj] floatValue];
    for ( NSDictionary *dict in array ) {
        if(max<[[dict objectForKey:obj] floatValue])max=[[dict objectForKey:obj] floatValue];
    }
    return max;
}

-(float)findMin:array arrayKey:obj {
    float min = [[[array objectAtIndex:0] objectForKey:obj] floatValue];
    for ( NSDictionary *dict in array ) {
        if (min > [[dict objectForKey:obj] floatValue])min = [[dict objectForKey:obj] floatValue];
    }
    return min;
}

如果在另一个类中,您将像这样访问方法:

GraphData *c=[[GraphData alloc] init];
float maxY=[c findMax:plotData arrayKey:[NSNumber numberWithInt:1]]; //1 or 0 depending on axis 
[c release];

0
你可以通过数组使用快速枚举。
NSUInteger  maxX = 0;
NSUInteger  minX = 0xFFFFFFFF; //(for 32 bit)
NSUInteger  maxY = 0;
NSUInteger  minY = 0xFFFFFFFF; //(for 32 bit)
for ( NSDictionary *dict in contentArray ) 
{
    NSUInteger   newX = [[dict objectForKey:@"x"] integerValue];
    NSUInteger   newY = [[dict objectForKey:@"y"] integerValue];


    if (maxX > newX) maxX = newX;
    if (minX > newX) minX = newX;
    if (maxY > newY) maxY = newY;
    if (minY > newY) minX = newY;
}

我不确定这样做是否更快,但是在最终任务中,您可以使用:maxX = MAX(maxX, newX); 等等... - Zev Eisenberg

0

//这个函数在NSMutableArrray中查找最大值和最小值 -(void)FindingMinAndMaxValueInNSMutableArray{

NSMutableArray *array=[[NSMutableArray alloc]initWithObjects:@"0.987",@"0.951",@"0.881",@"0.784",@"0.662",@"0.522",@"0.381",@"-0.265",@"-0.197", @"0.189",@"-0.233",@"0.310",@"0.402",@"0.402",@"0.988",@"0.633",@"0.661",@"0.656",@"0.617",@"0.634",@"0.690",@"0.767",@"0.836",nil];

NSLog(@"The Array Value is %@",array);
NSLog(@"The Array Count is %lu",(unsigned long)array.count);

NSNumber *maxValue = [array valueForKeyPath:@"@max.doubleValue"];
NSLog(@"The maxValue is %@",maxValue);
NSString *str=[NSString stringWithFormat:@"%@",maxValue];
NSInteger path=[array indexOfObject:str];
NSIndexPath *indexpath=[NSIndexPath indexPathForItem:path inSection:0];
NSLog(@"Max Value = %@ and index = %ld",maxValue,(long)indexpath.row);


NSNumber *minValue = [array valueForKeyPath:@"@min.doubleValue"];
NSLog(@"The minValue is %@",minValue);
NSString *str1 = [NSString stringWithFormat:@"%@",minValue];
NSInteger path1 = [array indexOfObject:str1];
NSIndexPath *indexPath1 = [NSIndexPath indexPathForItem:path1 inSection:0];
NSLog(@"Min Value =%@ and index = %ld",minValue,(long)indexPath1.row);

}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接