数组stockPriceDate1..4在整个代码中使用相同。在最后两个条目上获得l值错误。这是我的标点符号,我错了吗?
func sChart(chart: ShinobiChart!, dataPointAtIndex dataIndex: Int, forSeriesAtIndex seriesIndex: Int) -> SChartData! {
return seriesIndex == 0 ?
stockPriceData1[dataIndex]:
stockPriceData2[dataIndex];
stockPriceData3[dataIndex]; <- Expression resolves error...
stockPriceData4[dataIndex] <- Expression resolves error...
}
答案 0 :(得分:3)
让我们以不同的方式重写您的代码:
return seriesIndex == 0 ? stockPriceData1[dataIndex] : stockPriceData2[dataIndex]
let _ = stockPriceData3[dataIndex]
let _ = stockPriceData4[dataIndex]
请注意,返回值为stockPriceData1
或stockPriceData2
。接下来的两个stockPriceData3
和stockPriceData4
下标都会被忽略,因为您没有使用他们的结果。
代码肯定不会按照您的想法执行。
如果您想使用seriesIndex
来获取正确的数据数组,则需要使用switch
,例如
switch seriesIndex {
case 0:
return stockPriceData1[dataIndex]
case 1:
return stockPriceData2[dataIndex]
case 2:
return stockPriceData3[dataIndex]
case 3:
return stockPriceData4[dataIndex]
default:
return nil // should never happen
}
或使用数组:
let data = [stockPriceData1, stockPriceData2, stockPriceData3, stockPriceData4]
return data[seriesIndex][dataIndex]