Sunday, September 17, 2017

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

Leave a Comment

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.

3 Answers

Answers 1

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.

Answers 2

Have you tried to import to Test Target ?

enter image description here

Answers 3

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

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment