Reputation: 51
I have a class which is for bluetooth data receiving and I wonder to send the receiving data to class UIViewController and do the realtime plotting(CorePlot)
class BluetoothManager: NSObject {
func dataReceiving(value: [Int]){
for (var m=0 ; m<5 ; m++){
dataOne[m] = value[2*m]
dataTwo[m] = value[2*m+1]
}
MainController().plot(dataOne: [Int], dataTwo: [Int])
}
MainController class:
class MainController: UIViewController,CPTScatterPlotDataSource {
@IBOutlet weak var graphView: CPTGraphHostingView!
private var scatterGraph : CPTXYGraph? = nil
typealias plotDataType = [CPTScatterPlotField : Double]
private var dataForPlot = [plotDataType]()
func plot(dataOne: [Int], dataTwo: [Int]){
let newGraph = CPTXYGraph(frame: CGRectZero)
graphView.hostedGraph = newGraph
...
}
}
When the procedure goes to graphView.hostedGraph = newGraph
, there will have a fatal error:
unexpectedly found nil while unwrapping an Optional value
I have tried call the plot function in the MainController class, it was worked!
Can anybody help me to fix this problem? Thanks!
Upvotes: 0
Views: 333
Reputation: 285082
MainController()
creates a new instance of the MainController
class which is different from the object in Interface Builder.
You need the reference to the MainController
object in Interface Builder
Upvotes: 0
Reputation: 57124
You are creating a new instance of the MainController
in the line
MainController().plot(dataOne: [Int], dataTwo: [Int])
Therefore the @IBOutlet weak var graphView: CPTGraphHostingView!
is nil
- therefore your code crashes.
You must not create a new instance of the MainController
the way you currently do - either instantiate it via the storyboard, or pass in the correct nib file. For both cases you have to set the MainController as controller class in the IB and connect the outlet.
Upvotes: 1
Reputation: 1967
you have declared plot as an instance method, and you are calling it as a class method.
You need to change this line:
func plot(dataOne: [Int], dataTwo: [Int]){
To this line:
class func plot(dataOne: [Int], dataTwo: [Int]){
If you need to call it like you do it
Upvotes: 0