Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Use of undeclared type" in Swift, even though type is internal, and exists in same module

Tags:

xcode

swift

For me, I encountered this error when my test target did not have some swift files that my app build target had in compile sources. It was very confusing because the 'undeclared type' was being used in so many other places with no problem, and the error seemed vague. So solution there was of course to add the file containing the 'undeclared type' to the test target.


This has already been answered by @Craig Otis, but the issue is caused when the classes in question do not belong to the same targets, usually the test target is missing. Just make sure the following check boxes are ticked.


target membership

Edit

To see the target membership. Select your file then open the file inspector (⌥ + ⌘ + 1) [option] + [command] + 1

detailed description


Phew, finally diagnosed this. Somehow, the offending Swift file EditTaskPopoverController.swift was in two different build phases.

It was in Compile Sources properly, with all the other Swift files, but it was also, for some very strange reason, in the Copy Bundle Resources phase as well, along with all my XIB and image resources.

I have no idea how it got there, but removing it from the extra build phase resolved the issue.


In XCode menu Product->Clean and then Product->Build worked for me. I encountered this issue when added new ViewController to my project in new Group/Folder.


I had the exact same problem. Some of the files in my framework were not reachable from other classes within the same module.

For some reason the files that had been added to the framework in Xcode was not part of the Compile Sources. If your Swift file is not part of the compile sources you need to add them by tapping the + and selecting them in the popup.

Screenshot 1

Also make sure the file is part of the framework target. (The little box in the screenshot below should be checked)

Screenshot 2


The cause for me was a function name that began with same characters as a type:

@IBOutlet weak var tableView: CustomTableView!

and in the implementation I had a function beginning with CustomTableView

func CustomTableView(tableView: CustomTableView, dataForRow row:  Int) -> NSData {...}

The fix was to change the function signature so that it didn't begin with the same characters as the type (CustomTableView), e.g.:

func dataForRow(row: Int, tableView: CustomTableView) -> NSData {...}

This was a very misleading error message for the actual cause in my case.