Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

inaccessible due to 'internal' protection level

Tags:

swift

I am implementing the ScrollableGraphView library in my code (https://github.com/philackm/ScrollableGraphView) and experiencing the following problem:

To confirm to ScrollableGraphViewDataSource, when i add

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

to my code, Xcode gives the following error at "plot.identifier" :

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

Can anyone help what is going on?

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
}
}
like image 798
Amir Siddique Avatar asked Jul 09 '17 13:07

Amir Siddique


1 Answers

The identifier property of the Plot class does not have an explicit access level, so it receives the default access level of "internal". Internal properties can be accessed by from code within the defining module but not from code in another module.

Your code is not in the defining module, so it cannot access the internal identifier property.

The author of that framework should probably make a read-only public identifier property available.

Submitting an issue on the repo, as you have done, is a reasonable approach.

You can work around the problem by forking the repo and explicitly changing identifier to be public var identifier:String! although this is a bit of a hack.

identifier should really be public let identifier:String with an appropriate initialiser that the subclasses call. It looks like the author is intending Plot to be an "abstract" class by not providing an initialiser, but they then have to use an implicitly unwrapped identifier variable. An internal init(identifier:) function would be preferable in my opinion

like image 100
Paulw11 Avatar answered Nov 19 '22 21:11

Paulw11