OBJ-C:获取 NSMutableArray 中的最小值/最大值
我想获取 NSMutableArray 的最大值和最小值,以便我可以基于这些最大值和最小值创建核心图绘图空间和图表。
我的代码如下:
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 分配的空白?
I want to get the maximum and minimum values of a NSMutableArray so I can create a core-plot plotspace and graph based around those max and min values.
My code is as follows:
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()];
What do I fill in the blanks for xRange and yRange assignment if I want the graph to span only the space of what's specified in contentArray?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在这种情况下,您应该使用
-[CPPlotSpace scaleToFitPlots:]
。有关数组值的更一般计算,请继续阅读...这是键值编码和相关 数组运算符。在您的示例中,
数组运算符在
contentArray
上调用valueForKeyPath:
,这会通过在该数组的每个成员上调用相同的valueForKeyPath:
来构建一个数组。数组,键路径位于数组运算符右侧(即@min
和@max
)。然后,原始调用将给定的运算符应用于结果数组。您可以轻松地在NSArray
上定义一个类别,以返回最小/最大值的结构:In this case, you should use
-[CPPlotSpace scaleToFitPlots:]
. For more general calculations on array values, read on...This is an ideal use for Key-Value coding and associated array operators. In your example,
The array operators call
valueForKeyPath:
on thecontentArray
which gives an array built by calling the samevalueForKeyPath:
on each member of the array with the key path to the right of the array operator (i.e.@min
and@max
). The orignal call then applies the given operator to the resulting array. You could easily define a category onNSArray
to give you back a struct of min/max values:当您填充它时,您可以找到最大值和最小值,但它仅在您的代码片段中使用时才起作用。如果您想要更通用的东西,您应该简单地浏览列表并搜索它。
You can find max and min values when you fill it, but it would work just when used in your snippet. If you want something more generic you should simply go through the list and searching for it.
您可以通过数组使用快速枚举。
。
You could use fast enumeration through the array.
.
如果你有 NSMutableArray 的 NSDictionary 对象,你可以使用这个:
如果在另一个类中,你将访问这样的方法:
if you have NSMutableArray of NSDictionary objects you can use this:
You would access methods like this if in another class:
//这给出了 NSMutableArray 中的最大值和最小值
-(void)FindingMinAndMaxValueInNSMutableArray{
}
//This gives Max and Min Value in NSMutableArrray
-(void)FindingMinAndMaxValueInNSMutableArray{
}