Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift test unable to find Swift class property on Objective-C VC

I may be in compiler hell right here.

I'm implementing a Snapshot test in Swift, calling a property on an Objective-C VC, but that property is a class, written in Swift, bridged in.

In MyViewController.h:

@class TextEntryView;

@interface MyViewController: AbstractTextEntryViewController

@property (nonatomic, strong) TextEntryView *textEntryView;

@end

In TextEntryView.swift:

@objc(TextEntryView) class TextEntryView: UIView

And in my test, I'm trying to call

vc.textEntryView where vc is of type MyViewController and I'm getting the error:

value of type MyViewController has no member textEntryView

My bridging headers look good. If I add an NSString property to the .h file above, I'm able to reference it in the test. Also, when I command-click on MyViewController in my test, it takes me to the .h file rather than the .swift generated version of that file (which seems like a symptom of this problem).

I may be pushing Xcode 8 beyond its limits.

like image 776
Zack Shapiro Avatar asked Sep 08 '17 17:09

Zack Shapiro


3 Answers

You need to make sure you import your app at the top of the source file for your test. For example, if your app is called MyApp, insert at the top of the test file:

import MyApp

If I leave out the import, I get the same behavior you are seeing. Additionally, as long as the import is there, you shouldn't have to bother with bridging headers for the unit test.

like image 128
Charles Srstka Avatar answered Sep 21 '22 17:09

Charles Srstka


Have you tried to import to Test Target ?

enter image description here

like image 36
khoiNN Avatar answered Sep 19 '22 17:09

khoiNN


Since you already imported the Xcode-generated header file for your Swift code into Objective-C .m file.

Please also remove @objc annotation from TextEntryView class since it's a subclass of UIView thus accessible and usable in Objective-C. keeping the annotation @objc may cause a side effect.

To be accessible and usable in Objective-C, a Swift class must be a descendant of an Objective-C class or it must be marked @objc.

a simple case of "side-effect" is when a (swift) UIViewController subclass is marked @objc and used as custom subclass in storyBoard:

instantiateViewControllerWithIdentifier will instantiate a UViewController instead of the subclass we set in the storyBoard. with error Unknown class _TtC10AbcdViewController in Interface Builder file

like image 44
Idali Avatar answered Sep 22 '22 17:09

Idali