Swift test could not find Swift class property on Objective-C VC

I can be in the compiler here.

I run the Snapshot test in Swift, invoking a property in Objective-C VC, but this property is a class written in Swift, with a bridge.

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 get an error:

a value of type MyViewController does not have a textEntryView member

My bridge headings look good. If I add the NSString property to the .h file above, I can reference it in the test. Also, when I command -click on MyViewController in my test, it transfers me to the .h file and not to the .swift generated version of this file (which seems like a symptom of this problem).

I can push Xcode 8 beyond it.

+7
ios objective-c xcode swift
source share
3 answers

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

 import MyApp 

If I leave import , I get the same behavior that you see. Also, if import exists, you donโ€™t have to worry about the header jumper for unit test.

+1
source share

Have you tried to import into Test Target?

enter image description here

+1
source share

Since you already imported the header file generated by Xcode for your Swift code into an Objective-Cm file

Also remove the @objc annotation from the TextEntryView class, since the UIView subclass is available and available in Objective-C. retaining @objc annotation may cause a side effect.

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

A simple case of a โ€œside effectโ€ is when the subclass (swift) of the UIViewController tagged @objc and used as a custom subclass in storyBoard:

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

+1
source share

All Articles