如何在(多维?)数组中找到最小值和最大值

时间:2014-12-28 16:17:43

标签: ios arrays swift

我有一个在Swift

中创建的数组
class TableViewCell: UITableViewCell, TKChartDelegate {

    var xAxisDates = [] --> filled with fetch from Coredata
    var yAxisValues = [] --> filled with fetch from Coredata

    var maximumValue = maxElement(yAxisValues)   -> error: TableViewCell.Type' does not have a member named 'yAxisValues'

func chart (){
    var dataPointSeries = [TKChartDataPoint]()

    for var i = 0; i < xAxisDates.count; ++i {
    dataPointSeries.append(TKChartDataPoint(x: xAxisDates[i], y: yAxisValues[i]))

    println(dataPointSeries)
    }
<--more code setting up the chart-->
}

控制台输出为:

[x =&#39; 2014-11-25 02:00:00 + 0000&#39;,y =&#39; 61&#39;,name =&#39;(null)&#39; ,x =&#39; 2014-11-25 02:00:00 + 0000&#39;,y =&#39; 57&#39;,name =&#39;(null)&#39;]

我想找到y的最大值和最小值。

我已经尝试了maxElement(dataPointSeries(x)),但这不起作用。知道我怎么能这样做吗?我似乎无法找到任何解决方案。

1 个答案:

答案 0 :(得分:2)

您想要的只是y元素的最小值和最大值,不需要在多维数组的上下文中考虑这一点。您所需要做的就是:

let minimumYValue = minElement(yAxisValues)
let maximumYValue = maxElement(yAxisValues)
相关问题