2

Say I have the following in an Objective C implementation file (MyVC.m):

@interface MyVC ()
  @property (nonatomic, weak) IBOutlet UILabel *titlelabel;
@end

How can I expose this private property to Swift (test) code? The following causes compiler errors:

extension LTConversationSelectionVC {
   @IBOutlet weak var assetBar: LTAssetBar?
}
pr1001
  • 21,727
  • 17
  • 79
  • 125

3 Answers3

2

Unfortunately, there isn't a way to do this purely with Swift. However, you can expose the property in an Objective-C file in your test target and then put that in your test target's bridging header.

Add MyVC+Tests.h to your test target:

@interface MyVC (Tests)
  @property (nonatomic, weak) IBOutlet UILabel *titlelabel;
@end

MyAppTests-Bridging-Header.h

#import "MyVC+Tests.h"

Then, in your Swift code in the test target, you can use the titleLabel property as expected.

let vc = MyVC()
vc.titleLabel.text = "Hello, world!"
Isaac Overacker
  • 1,385
  • 10
  • 22
1

From apple doc


Extensions can add new computed properties, but they cannot add stored properties, or add property observers to existing properties.


Apple documentation Link here

bsarr007
  • 1,914
  • 14
  • 14
0

You can do the same property-in-category trick from the Bridging Header file. Just forward any class that calls swift library. That should take care of the problem.

Angel
  • 11
  • 3