0

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?

heimi
  • 499
  • 7
  • 16

1 Answers1

0

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.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • My Problem: I have the app (CNC-Machine) running in Objective-C for many years and want to run it integrated in a swift app. All Outlets are defined in the rAVRView class. When I replace the above to ```@IBOutlet weak var AVR:rAVRview!``` I get : Cannot find 'rAVRView' in scope I am not very familiar with this sort of problem. – heimi Jul 24 '22 at 15:49
  • 1
    Your examples do not appear to be the exact code and error messages. There is different capitalization of rAVRview (which by itself would cause these errors). In your original example, the declaration is non-optional, but the calling line is optional. This suggests you're not showing us the real code. I don't know how to help you in that case. Teaching you all the details of integrating Swift and ObjC may be beyond what Stack Overflow can provide, but at a minimum, you have to give us the actual code and errors. – Rob Napier Jul 24 '22 at 17:12
  • Thank you for your answer. I think I have to dive deeper into the problem before asking questions. The code of the original app is about 20'000 lines, so I thought to transfer it into a swift app would be easier than translating the Objective-C-code. – heimi Jul 24 '22 at 17:38