所以我有一个这样的功能:

    var localIndices: [ChartHighlight] = []

    for globalHighlight in globalIndices
    {
        if let globalSet = data?.dataSets[globalHighlight.dataSetIndex]
        {
            if globalSet.dynamicType === dataSetType
            {
                let chartData = getChartData(dataSetType)

                if (chartData == nil)
                {
                    return globalIndices
                }

                let localdataSetIndex = chartData!.dataSets.indexOf(globalSet)
                if (localdataSetIndex != nil)
                {
                    localIndices.append(ChartHighlight(xIndex: globalHighlight.xIndex, dataSetIndex: localdataSetIndex!, stackIndex: globalHighlight.stackIndex))
                }
            }
            else
            {
                return globalIndices
            }
        }
    }

编译器抱怨
let localdataSetIndex = chartData!.dataSets.indexOf(globalSet)
这么说吧
无法将“ichartDataset”类型的值转换为所需的参数类型“@noescape(ichartDataset)throws->bool”
我很困惑为什么我不能用NormalindexOf()来处理[IChartDataSet]。这与chartData!.dataSets[IChartDataset]IChartDatasetprotocol类型相关吗?
我没看到斯威夫特医生提到这个,或者我错过了…

最佳答案

要使用带有element作为输入参数的indexOf方法,您的元素类型dataSets必须符合IChartDataSet协议,如您从Equatable扩展定义中看到的:

    extension CollectionType where Generator.Element : Equatable {
       /// Returns the first index where `value` appears in `self` or `nil` if
        /// `value` is not found.
        ///
        /// - Complexity: O(`self.count`).
        @warn_unused_result
        public func indexOf(element: Self.Generator.Element) -> Self.Index?
    }

或者可以使用谓词闭包:
let localdataSetIndex = chartData!.dataSets.indexOf { $0 === globalSet }

关于swift - swift中的编译器错误indexOf(),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36738040/

10-09 16:28