gpt4 book ai didi

swift - 由于 'internal' 保护级别而无法访问

转载 作者:可可西里 更新时间:2023-11-01 00:40:12 24 4
gpt4 key购买 nike

我在我的代码中实现 ScrollableGraphView 库 ( https://github.com/philackm/ScrollableGraphView ) 并遇到以下问题:

确认 ScrollableGraphViewDataSource,当我添加

func value(forPlot plot: Plot, atIndex pointIndex: Int) -> Double 

对于我的代码,Xcode 在“plot.identifier”处给出以下错误:

'identifier' is inaccessible due to 'internal' protection level. 

任何人都可以帮助发生了什么事吗?

import UIKit

import ScrollableGraphView

class ViewController: UIViewController, ScrollableGraphViewDataSource {

// Class members and init...

var linePlotData: [Double] = [1, 2, 3, 4, 5]// data for line plot
var barPlotData: [Double] = [1, 2, 3, 4, 5]// data for bar plot
var xAxisLabels: [String] = ["Jan", "Feb", "Mar", "Apr", "May"]// the labels along the x axis

var numberOfPointsInGraph = 5

override func viewDidLoad() {
super.viewDidLoad()

let graph = ScrollableGraphView(frame: self.view.frame, dataSource: self)

// Graph Configuration
// ###################

graph.backgroundColor = UIColor.white
graph.shouldAnimateOnStartup = true

// Reference Lines
// ###############

let referenceLines = ReferenceLines()
referenceLines.positionType = .relative
referenceLines.relativePositions = [0, 0.5, 0.8, 0.9, 1]

graph.addReferenceLines(referenceLines: referenceLines)

// Adding Plots
// ############

let linePlot = LinePlot(identifier: "linePlot")
linePlot.lineWidth = 5
linePlot.lineColor = UIColor.black

let barPlot = BarPlot(identifier: "barPlot")
barPlot.barWidth = 20
barPlot.barLineColor = UIColor.gray

graph.addPlot(plot: linePlot)
graph.addPlot(plot: barPlot)

self.view.addSubview(graph)
}

// Implementation for ScrollableGraphViewDataSource protocol
func value(forPlot plot: Plot, atIndex pointIndex: Int) -> Double {

switch (plot.identifier) {
case "linePlot":
return linePlotData[pointIndex]
break
case "barPlot":
return barPlotData[pointIndex]
break
}
}

func label(atIndex pointIndex: Int) -> String {
return xAxisLabels[pointIndex]
}

func numberOfPoints() -> Int {
return numberOfPointsInGraph
}
}

最佳答案

Plot 类的identifier 属性没有明确的访问级别,因此它接收默认访问级别“internal”。内部属性可以通过定义模块中的代码访问,但不能通过另一个模块中的代码访问。

您的代码不在定义模块中,因此它无法访问内部 identifier 属性。

该框架的作者可能应该提供一个只读的公共(public) identifier 属性。

像您所做的那样,在 repo 上提交问题是一种合理的方法。

您可以通过 fork 存储库并将 identifier 显式更改为 public var identifier:String! 来解决此问题,尽管这有点 hack。

identifier 实际上应该是 public let identifier:String 并带有子类调用的适当初始化程序。看起来作者打算通过不提供初始化程序将 Plot 设为“抽象”类,但他们随后必须使用隐式展开的 identifier 变量。在我看来,内部 init(identifier:) 函数更可取

关于swift - 由于 'internal' 保护级别而无法访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44996982/

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