179

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?

Craig Otis
  • 31,257
  • 32
  • 136
  • 234
  • I've had a lot of cases where the actual error is a few lines (or more) further down, and Xcode erroneously tags something innocuous with an error message. See if you can get that error to go away by erasing the rest of your code, then add it back bit by bit... – Nate Cook Aug 22 '14 at 02:48
  • Thanks Nate - unfortunately I remove the `var` declarations, and get down to a passing build. Then I add *one* of the declarations at the top, and the error is back immediately: http://imgur.com/VUUBK2K – Craig Otis Aug 22 '14 at 10:04

33 Answers33

204

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.

mattorb
  • 3,075
  • 3
  • 22
  • 16
  • It seems like the file which shows "undeclared type" is in the test target, but the "undeclared type" isn't in test target. so it's undeclared type in test target environment. – hzwzw Dec 25 '16 at 14:29
  • Note also you could be missing a @testable import. – Roy Falk Jul 19 '19 at 07:47
190

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

Community
  • 1
  • 1
Edwin
  • 3,812
  • 2
  • 21
  • 16
38

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.

Craig Otis
  • 31,257
  • 32
  • 136
  • 234
28

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.

Oleg Novosad
  • 2,261
  • 1
  • 27
  • 28
14

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

Groot
  • 13,943
  • 6
  • 61
  • 72
12

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.

Andrew
  • 7,630
  • 3
  • 42
  • 51
9

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).

Community
  • 1
  • 1
lindon fox
  • 3,298
  • 3
  • 33
  • 59
  • Restarting worked for me, although the error popped up again after a successful build, without any further code changes. – Uncommon Mar 07 '17 at 19:53
6

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 .

Suraj K Thomas
  • 5,773
  • 4
  • 52
  • 64
5

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.

Community
  • 1
  • 1
Nate Uni
  • 933
  • 1
  • 12
  • 26
5

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
Ratz
  • 109
  • 1
  • 7
4

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

Yusuf X
  • 14,513
  • 5
  • 35
  • 47
4

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])
Rindom
  • 41
  • 4
3

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

Babac
  • 931
  • 11
  • 21
3

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. :)

Maxwell
  • 6,532
  • 4
  • 37
  • 55
3

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

tsuchi.one
  • 31
  • 2
2

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.

Almin
  • 29
  • 2
2

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".

2

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!

zeeple
  • 5,509
  • 12
  • 43
  • 71
2

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.

quellish
  • 21,123
  • 4
  • 76
  • 83
2

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

coder
  • 8,346
  • 16
  • 39
  • 53
Jirui
  • 139
  • 1
  • 6
2

Cleaning the project solved my problem.

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

1

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

cmario
  • 605
  • 1
  • 7
  • 22
1

as others mentioned well and in this thread

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

Community
  • 1
  • 1
Hamed Nova
  • 1,061
  • 9
  • 15
1

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.

Oliver Pearmain
  • 19,885
  • 13
  • 86
  • 90
1

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

Sultan Ali
  • 2,497
  • 28
  • 25
1

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.

1

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

1

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.

Wangdu Lin
  • 913
  • 1
  • 9
  • 19
1

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.

Alirza Eram
  • 134
  • 11
  • 1
    This may be a stupid question but can you explain more about that? Does this happen with duplicate swift files as well, or duplicate groups etc..? It worked but I like to know more about why it works than just accepting that it works. Thanks for the solution by the way! – christopherson Dec 06 '19 at 19:29
  • 2
    During copy and pasting you may accidentally duplicate a folder, module or etc. Everything should be unique in Xcode. I think that's the reason – Alirza Eram Jan 09 '21 at 06:37
1

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.

fs_tigre
  • 10,650
  • 13
  • 73
  • 146
0

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)

Tim
  • 1,877
  • 19
  • 27
0

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

lotype
  • 593
  • 3
  • 8
0

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

GP89
  • 6,600
  • 4
  • 36
  • 64