不能调用带有“String”类型的参数列表的CGFloat类型的初始化器

时间:2021-06-17 23:09:22

I am making a bar chart from JBChart. I would like to change the chartData to String. please help me do it.

我正在用JBChart做一个条形图。我想把chartData改为String。请帮我做。

here's the chartData code: var chartData = [1, 2, 3]

这是chartData代码:var chartData = [1,2,3]

Then I want to change it to a String from a UILabel from other view controller.

然后我想把它从其他视图控制器的UILabel中更改为字符串。

var chartData = [score1, score2, score3]

But I always get this error (Cannot invoke initializer for type CGFloat with an argument list of type 'String')in this code:

但是我总是在这段代码中得到这个错误(不能调用带有‘String’类型参数列表的CGFloat类型的初始化器):

    public func barChartView(_ barChartView: JBBarChartView!, heightForBarViewAt index: UInt) -> CGFloat {
        return CGFloat(chartData[Int(index)])
    }

1 个解决方案

#1


1  

The error is clearly saying that CGFloat doesn't have initializer that accept String as argument. You can use wrapped around like first convert String to Float and then convert Float to CGFloat.

错误显然是说CGFloat不具有接受字符串作为参数的初始化器。你可以像第一次将字符串转换为Float,然后将Float转换为CGFloat。

public func barChartView(_ barChartView: JBBarChartView!, heightForBarViewAt index: UInt) -> CGFloat {

    if let value = Float(chartData[Int(index)]) {
        return CGFloat(value)
    }
    return 0
}

Note: Be sure that this String have number as value otherwise it will return 0 for height.

注意:请确保该字符串的值为number,否则其高度将返回0。

#1


1  

The error is clearly saying that CGFloat doesn't have initializer that accept String as argument. You can use wrapped around like first convert String to Float and then convert Float to CGFloat.

错误显然是说CGFloat不具有接受字符串作为参数的初始化器。你可以像第一次将字符串转换为Float,然后将Float转换为CGFloat。

public func barChartView(_ barChartView: JBBarChartView!, heightForBarViewAt index: UInt) -> CGFloat {

    if let value = Float(chartData[Int(index)]) {
        return CGFloat(value)
    }
    return 0
}

Note: Be sure that this String have number as value otherwise it will return 0 for height.

注意:请确保该字符串的值为number,否则其高度将返回0。