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

XcodeSwift

Xcode Problem Overview


I have a type in my module:

import Cocoa

class ColoredDotView : NSView {
   ...
}

It is used in a number of different classes with no issue:

class EditSubjectPopoverController : NSObject {

    @IBOutlet internal var subjectColorDotView : ColoredDotView!
    ...
}

But for some reason, when I use it in one specific class, I have compilation errors on the type:

class EditTaskPopoverController : NSObject {
    
    @IBOutlet internal var lowPriorityDotView : ColoredDotView! // Error here
    @IBOutlet internal var medPriorityDotView : ColoredDotView! // And here...
    @IBOutlet internal var highPriorityDotView : ColoredDotView! // And here...
    ...
}

The compilation error is:

> EditTaskPopoverController.swift:15:49: Use of undeclared type > 'ColoredDotView'

Which I don't understand. It's the first compilation error in the file, and the rest of the errors are all symptomatic of the first. Further, there are no other files with compilation errors. I don't understand why the type is undeclared, as the file is in the same module:

enter image description here

I have tried cleaning the project, cleaning the build folder, and restarting Xcode, to no avail. What potential missteps can cause an undeclared type compiler error in Swift?

Xcode Solutions


Solution 1 - Xcode

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.

Solution 2 - Xcode

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

Solution 3 - Xcode

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.

Solution 4 - Xcode

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.

Solution 5 - Xcode

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

Solution 6 - Xcode

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.

Solution 7 - Xcode

In case anyone encounters a similar problem but the Compile Sources fix does not solve the problem, restarting Xcode may (it worked for me). My version of Xcode is Version 6.1 (6A1052d).

Solution 8 - Xcode

> Sometimes errors can be very silly > > Before checking all the solutions up here , make sure you have > imported all the basic stuff

     import Foundation
     import UIKit

> It is quite a possibility that when you import some files from outside > to your project, which may miss this basic things as I experienced > once .

Solution 9 - Xcode

In my app I have app delegate and other classes that need to be accessed by the tests as public. As outlined here, I then import my my app into my tests.

When I recently created two new classes ,their test targets were both the main and testing parts. Removing them from their membership from the tests solved the issue.

Solution 10 - Xcode

In my case, the TestTarget's compile sources were having files from the Main Target.

Ref:

Test Target contains files from main target in compile sources tab

Why this happens ?

  • This happens since we check the TestTarget association while creating the file

  • Or manually checking this option from the inspector.

    Ref:

Create File - Test Target is Checked

How did i resolve ?

  • I removed the main target's files from compile source of Test Target

Solution 11 - Xcode

I tried many of the solutions offered here, but eventually deleted the file and created it again, and Xcode was mollified :/

Solution 12 - Xcode

This can also happen if you by accident capitalize the parameter name, and call it the same as the object.

class func didRecieveData(BlockItems: [BlockItems])

Solution 13 - Xcode

This might help someone.

I've created new test project with Core Data called "CoreData". Shortly I've got "Use of undeclared type" for NSManagedObjectContext and other Core Data classes. After several attempts of importing, adding to Build phases, etc. I've deleted project and started new one called "TestingCoreData" and it all worked well.

Don't name (test) Projects like name of the Classes

Solution 14 - Xcode

This can also happen if you have a function with the same name as an object type in your signature. For example:

class func Player(playerObj: Player)

will cause the compiler to get confused (and rightfully so) since the compiler will first look locally within the file before looking at other files. So it looks at "Player" in the signature and thinks, that's not an object in this scope, but a function, so something is wrong.

Perhaps this is a good reason why I shouldn't capitalize class functions. :)

Solution 15 - Xcode

I got this error message in Xcode 8 while refactoring code in to a framework, it comes out that I forgot to declare the class in the framework as public

Solution 16 - Xcode

Maybe you have added class with some "FirstNameClass" and after that manually rename to "ColoredDotView". Try to copy content of "ColoredDotView" class to clipboard, remove "ColoredDotView" from project and add agin.

This id fix similar problem from me.

Solution 17 - Xcode

In my case was a mistake made by me. I added a new file as "OS X>Source>Cocoa Class", instead of "iOS>Source>Cocoa Touch Class".

Solution 18 - Xcode

In my case, this was caused by a subclass name being used in the very next line as a variable name with a different type:

var binGlow: pipGlow = pipGlow(style: "Bin")
var pipGlow: PipGlowSprite = PipGlowSprite()

Notice that in line 1, pipGlow is the name of the subclass (of SKShapeNode), but in line two, I was using pipGlow as a variable name. This was not only bad coding style, but apparently an outright no-no as well! Once I change the second line to:

var binGlow: pipGlow = pipGlow(style: "Bin")
var pipGlowSprite: PipGlowSprite = PipGlowSprite()

I no longer received the error. I hope this helps someone!

Solution 19 - Xcode

When testing Swift code that belongs to the application, first make sure the test target is building the application as a dependency. Then, in your test, import the application as a module. For example:

@testable import MyApplication

This will make the Swift objects that are part of the application available to the test.

Solution 20 - Xcode

In my case, that was caused by swift files's Text Encoding. One file showed 'No Explicit Encoding', and after convert that to 'UTF-8', problem solved.

And the reason why file's text encoding is not explicit is that I copied all code from other swift file.

No Explicit Encoding Screenshot

enter image description here

UTF-8 Screenshot

enter image description here

Solution 21 - Xcode

Cleaning the project solved my problem.

Steps: Product -> Clean(or Shift + Cmd + K)

Solution 22 - Xcode

In my case I wanted to add a method with a custom swift object as a type parameter, and the name I gave to the variable in the parameter was exactly the same as the custom object class name

The problems was something like this:

func moveBlob(**blob** : blob){
    ...code
}

The part in bold characters was causing the error of undeclared type

Solution 23 - Xcode

as others mentioned well and in this thread

use of unneeded swift files in "copy bundle resources" screen shot for helping

Solution 24 - Xcode

Like others, it was some unrelated code that was causing the @testable to malfunction.

In my test target there was an Objective-C header file that had

@import ModuleUnderTest;

I removed this line (because the import was actually unnecessary) and miraculously @testable starting working again.

I was only able to track this down but removing everything from my project and adding it back in bit by bit until it failed. Eventually I found the problem line of code.

Solution 25 - Xcode

if you are accessing it from different module or Target then you just need it to public it

Solution 26 - Xcode

In case someone makes the same silly mistake I did...

I was getting this error because in renaming my source file, I accidentally removed the. from the filename and so the compiler treated the file as a plain text file and not as source to compile.

so I meant to rename the file to MyProtocol.swift but accidently named it MyProtocolswift

It's a simple mistake, but it was not readily obvious that this is what was going on.

Solution 27 - Xcode

Not adding the correct import delcaration can also be an obvious miss. For me, I had simply omitted to import PriorityUIKit.

Solution 28 - Xcode

My situation is that I drag a new file XXView.swift into the project. And declare a View type to be XXView then the error "use of undeclared type....".

I just try to add my XXView.swift to the test target which it solved the error. But I didn't want my UI Class involved in the test target.

Finally, I found my ViewController already in the test target which should not happen. ( I think because I create the VC by an xctemplate therefore, it automatically be included in the test target)

I remove the view controller from the test target, and then my XXView is now no need to add to the test target.

Conclusion: make sure all your related files should also uncheck the test target.

Solution 29 - Xcode

After spending an hour on this error I found that the module file is duplicated. delete the extra file, and shift+cmd+k to clean and the error is gone.

Solution 30 - Xcode

In my case, the issue was with a new class not being recognized. I solved the issue by deleting the class and re-adding it but this time checking the Watch App Extension option when creating the new class.

enter image description here

Please note that I do have a Watch App Extension in my Application.

Solution 31 - Xcode

Had an error with type Int, due to referencing to a local case in my enum called .Int

func foo() {
   switch self {
      case .Int(var info):
         // ..... some other code
   }
}

There error was here

someFuncWithInt(parameter: Int)

Fixed it with

someFuncWithInt(parameter: Swift.Int)

Solution 32 - Xcode

There are seemingly multiple reasons for this issue. Following the accepted answer did not solve it. For me it was Product > Clean Build Folder

Solution 33 - Xcode

The issue for me was the type I was using was in objective-c in a swift project and I had forgotten to import the type's objective-c header file in the bridging header file

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCraig OtisView Question on Stackoverflow
Solution 1 - XcodemattorbView Answer on Stackoverflow
Solution 2 - XcodeEdwinView Answer on Stackoverflow
Solution 3 - XcodeCraig OtisView Answer on Stackoverflow
Solution 4 - XcodeOleg NovosadView Answer on Stackoverflow
Solution 5 - XcodeGrootView Answer on Stackoverflow
Solution 6 - XcodeAndrewView Answer on Stackoverflow
Solution 7 - Xcodelindon foxView Answer on Stackoverflow
Solution 8 - XcodeSuraj K ThomasView Answer on Stackoverflow
Solution 9 - XcodeNate UniView Answer on Stackoverflow
Solution 10 - XcodeRatzView Answer on Stackoverflow
Solution 11 - XcodeYusuf XView Answer on Stackoverflow
Solution 12 - XcodeRindomView Answer on Stackoverflow
Solution 13 - XcodeBabacView Answer on Stackoverflow
Solution 14 - XcodeMaxwellView Answer on Stackoverflow
Solution 15 - Xcodetsuchi.oneView Answer on Stackoverflow
Solution 16 - XcodeAlminView Answer on Stackoverflow
Solution 17 - XcodeJavier Calatrava LlaveríaView Answer on Stackoverflow
Solution 18 - XcodezeepleView Answer on Stackoverflow
Solution 19 - XcodequellishView Answer on Stackoverflow
Solution 20 - XcodeJiruiView Answer on Stackoverflow
Solution 21 - XcodeAnshul TiwariView Answer on Stackoverflow
Solution 22 - XcodecmarioView Answer on Stackoverflow
Solution 23 - XcodeHamed NovaView Answer on Stackoverflow
Solution 24 - XcodeOliver PearmainView Answer on Stackoverflow
Solution 25 - XcodeSultan AliView Answer on Stackoverflow
Solution 26 - XcodeMarc RenaudView Answer on Stackoverflow
Solution 27 - XcodeStampView Answer on Stackoverflow
Solution 28 - XcodeWangdu LinView Answer on Stackoverflow
Solution 29 - XcodeAlirza EramView Answer on Stackoverflow
Solution 30 - Xcodefs_tigreView Answer on Stackoverflow
Solution 31 - XcodeTimView Answer on Stackoverflow
Solution 32 - XcodelotypeView Answer on Stackoverflow
Solution 33 - XcodeGP89View Answer on Stackoverflow