I have an app in Swift4 with some Objective-C-objects integrated with a Bridging-Header. That works fine sending data to the swift part with notifications.
I want to get data from within a swift function in rCNCViewController
from its Objective-C-object AVR
.
That class has an instance variable motorsteps
that is initialized to 48 in awakeFromNib and can be used in the whole class.
class in swift:
class rViewController: NSViewController, NSWindowDelegate
{
}
Subclass:
class rCNCViewController:rViewController
{
...
var AVR = rAVRview()
...
Class in Objectiv-C-part:
@interface rAVRview:NSViewController <NSTableViewDataSource,NSTableViewDelegate>
{
...
IBOutlet id xy;
...
}
// variables
...
int motorsteps;
...
@end
Get-function in AVR:
- (int)motorsteps
{
return motorsteps;
}
Calling function in swift part:
guard let steps:Int32 = AVR?.motorsteps() else {return}
The returned value of steps
is 0.
Observing the function call to AVR
in the debugger shows that motorsteps in AVR is 0.
The other objects of AVR are all 0x00.
Changing motorsteps()
to returning a constant value returns that value correctly.
After the call from the swift part, the value of the variable motorsteps
in AVR
is unchanged 48.
Why are the AVR objects in the call from the swift part not valid?
awakeFromNib
is only called when an object is loaded from a XIB or Storyboard file. You're creating AVR
using its init
instead:
var AVR = rAVRview()
You should not expect AVR's awakeFromNib
to be called in this case. If you're using a Storyboard (or standalone XIB), then you would define this as an @IBOutlet
and connect it in the Storyboard editor. If you're manually creating these views, then you will need to initialize them in initWithFrame:
(or potentially init:
), not in awakeFromNib
.