gpt4 book ai didi

ios - 错误 : Generic parameter 'R.Generator.Element' cannot be bound to non-@objc protocol type 'AnyObject'

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:26:49 24 4
gpt4 key购买 nike

我正在查询 HealthKit 并将其保存到 CoreData。我在一个单独的类中获取数据。在 TableViewController 中,我将数据附加到数组:

if NSUserDefaults.standardUserDefaults().boolForKey("weightSwitch") == true {
xAxisDatesArray.append(cdFetchWeight.queryCoreDataDate())
yAxisValuesArray.append(cdFetchWeight.queryCoreDataData())

并将其传递给 tableView.dequeueReusableCellWithIdentifier

myCell.xAxisDates = xAxisDatesArray[indexPath.row]
myCell.yAxisValues = yAxisValuesArray[indexPath.row]

在 UITableViewCell 中,我初始化变量(yAxisValues、xAxisDates)并将它们传递到图表库中,该图表库采用 x 和 y 值并绘制图表。

class TableViewCell: UITableViewCell, TKChartDelegate {

var xAxisDates = []
var yAxisValues = []

plot....

我需要获取 yAxisValues 的最小值和最大值,以便为数据设置适当的 y 轴范围。

我尝试使用以下代码获取最小值和最大值:

func rangeMinAndMax(){
let minYvalue = minElement(yAxisValues)
let maxYvalue = maxElement(yAxisValues)
}

但这会产生错误:通用参数“R.Generator.Element”不能绑定(bind)到非@objc 协议(protocol)类型“AnyObject”

- 问题:为什么以及如何修复?

任何帮助将不胜感激!

最佳答案

在这种情况下要做的事情就是扔掉所有误导性的渣滓,将其归结为最简单的情况:

let arr : [AnyObject] = [1,2,3]
let min = minElement(arr) // same error: "Generic parameter blah-de-blah..."

所以,您看,minElement 不适用于 AnyObject 数组,这就是错误告诉您的内容。原因很明显:AnyObject 不是 Comparable。一堆 AnyObject 事物没有“最小值”;一个 AnyObject “小于”另一个 AnyObject 的整个概念是未定义的。您需要将数组转换为 minElement 可以处理的数组,即某种 Comparable。

例如,在该代码中,我可以这样解决问题:

let arr : [AnyObject] = [1,2,3]
let min = minElement(arr as [Int])

这就是您需要做的事情。当然,你归结为什么取决于这些元素实际上是什么。在我看来,好像可能分别是 Double 和 NSDate,但这只是一个猜测;我不知道你的数组里有什么。你这样做(大概)。请注意,NSDate 不是 Comparable,因此您需要做更多的工作。

关于ios - 错误 : Generic parameter 'R.Generator.Element' cannot be bound to non-@objc protocol type 'AnyObject' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27679841/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com