1

I'm trying to create a simple LineChart from the Charts framework

I have successfully (at least I think I did it properly) used Carthage to link the binary to my XCode project so that I can use Charts library.

I created a UIView in my storyboard and set the Class and Module to the proper values: enter image description here

I created an outlet in the proper UIViewController class:

    @IBOutlet weak var lineChartView: LineChartView!

I added the delegate to the class:

    class LineChartViewController: UIViewController, ChartViewDelegate {

In the viewDidLoad() function I tried to set the delegate:

    lineChartView.delegate = self

*** this is the line that creates the 'fatal error' It's as if the outlet is not set properly, or the Class/Module is not set properly.

If I check Connection Inspector, the referencing outlet is showing as connected: enter image description here

I'm stumped as to what the problem could be so any help would be greatly appreciated! Thank you

M. Black
  • 353
  • 1
  • 4
  • 20
  • Did you check similiar threads and try the solutions provided? For example https://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu –  Sep 03 '17 at 19:44
  • Yes, I've looked through many different threads. I understand what the error suggest - that the lineChartView variable is somehow nil, despite the fact that I can see all of the hookups/outlets and they are done properly. What I don't understand is how a value is not assigned as soon as the LineChartViewController is created. – M. Black Sep 03 '17 at 19:57
  • You should handle the exception as the thread I linked to avoid crashes on nil. And check here maybe helps you with the rest of the issue(?) https://stackoverflow.com/questions/29321383/iboutlet-is-nil-but-it-is-connected-in-storyboard-swift , I don't code in Swift, but this question pops up on SO alot of times so , if it does not solve it for you hope someone else can give you the right direction . GL –  Sep 03 '17 at 20:11
  • Did you set the custom class for your view controller in Storyboard? – Vini App Sep 07 '17 at 07:42

3 Answers3

1

I never found a reason as to why objects in the storyboard were not able to access the 'Charts' Module. So, if anyone else is coming up with the same problem, what I did to rectify this was to build the LineChartView programmatically. I did not have any problems instantiating from the proper class and building a line chart.

    lineChartView = LineChartView(frame: CGRect(x: 0, y: 0, width: (view?.frame.width)!, height: (view?.frame.height)!))
    lineChartView?.delegate = self
    self.view.addSubview(lineChartView!)

I would also recommend taking a look at the documentation and examples here:

https://github.com/danielgindi/Charts

Some of the examples are still written in objective-C, but solutions are generally easy enough to work out by converting to Swift.

I would also recommend taking a look at MPAndroidChart which is where Charts is based from. There is much more documentation and examples you can find online to point you in the right direction.

https://github.com/PhilJay/MPAndroidChart

M. Black
  • 353
  • 1
  • 4
  • 20
1

I had the same issue... My solution was to create my outlet like this:

 @IBOutlet var lineChartView: LineChartView! = LineChartView()

Probably not the best practice, but it works

0

You are setting delegate in incorrect manner. Please go through the below link, this will help you to understand and set the right delegate.

Examples of Delegates in Swift 3

Priya
  • 351
  • 1
  • 14
  • I understand what delegates are in Swift 3. Why do you think I'm 'setting the delegate in incorrect manner'? I've gone through several different tutorials and each suggest to set the delegate in the way I did above. – M. Black Sep 04 '17 at 13:10
  • I am also using this framework. if you could share your complete code then I can see. – Priya Sep 04 '17 at 13:17
  • None of the code after the lineChartView.delegate = self really matters because that is where it crashes (which is in the ViewDidLoad method). If I make the lineChartView programmatically then it works fine (including the delegate) – M. Black Sep 04 '17 at 13:47