Reputation: 16166
I have an Objective-C class in my app (MyClass
). In a Swift file, I added an extension that makes it conform to the NSFilePromiseProviderDelegate
protocol/interface:
extension MyClass: NSFilePromiseProviderDelegate {
public func filePromiseProvider(_ filePromiseProvider: NSFilePromiseProvider, fileNameForType fileType: String) -> String {
...
}
public func filePromiseProvider(_ filePromiseProvider: NSFilePromiseProvider, writePromiseTo url: URL, completionHandler: @escaping (Error?) -> Void) {
...
}
public func operationQueue(for filePromiseProvider: NSFilePromiseProvider) -> OperationQueue {
...
}
}
My app is compiling fine, but I have a unit test bundle (what used to be called a "Logic Test" bundle in Xcode) that also compiles all of my classes outside the app bundle. When I add the Swift file that contains this extension, I get a compiler error in my unit tests:
<path>/NSPasteboard+ACAdditions.swift:205:18: error: redundant conformance of 'MyClass' to protocol 'NSFilePromiseProviderDelegate'
extension MyClass: NSFilePromiseProviderDelegate {
^
AppName.MyClass:1:11: note: 'MyClass' declares conformance to protocol 'NSFilePromiseProviderDelegate' here
extension MyClass : NSFilePromiseProviderDelegate {
^
It makes it look like the Swift file is somehow getting included in the unit test bundle twice, but I don't know how that would be possible. When I uncheck the unit test target from the "Target Membership" section of the File Inspector, I get other errors for classes in that file not being defined.
How do I clear this up to get my unit tests to compile again and test classes that need this file?
Upvotes: 11
Views: 651
Reputation: 11
If a swift beginner stumbled upon this issue, you properly did the same mistake as I:
I got an error when trying to access my classes within my tests. So I through I have to include all my classes in the main project and the testing project to use them in my test classes.
Don't target your main classes in your testing project! Keep your main code in the main project only. Instead add @testable import PROJECTNAME
to your test files and you are able to access your main code again.
I hope this will help you.
Upvotes: 1
Reputation: 16166
As a workaround, I moved the class extension into a different file that's not included in the unit test bundle, since it's only required for drag-and-drop, which the tests don't touch. I'm still curious what's causing this though, and how to get this extension into my tests, in case it were something that I did want to test.
Upvotes: 4