Reputation: 32104
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:
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?
Upvotes: 179
Views: 230000
Reputation: 6740
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
Upvotes: 0
Reputation: 10748
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.
Please note that I do have a Watch App Extension in my Application.
Upvotes: 1
Reputation: 613
There are seemingly multiple reasons for this issue. Following the accepted answer did not solve it. For me it was Product > Clean Build Folder
Upvotes: 0
Reputation: 178
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.
Upvotes: 1
Reputation: 913
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.
Upvotes: 1
Reputation: 5883
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 .
Upvotes: 6
Reputation: 119
In my case, the TestTarget's compile sources were having files from the Main Target.
Ref:
Why this happens ?
This happens since we check the TestTarget association while creating the file
Or manually checking this option from the inspector.
Ref:
How did i resolve ?
Upvotes: 5
Reputation:
Not adding the correct import delcaration can also be an obvious miss. For me, I had simply omitted to import PriorityUIKit.
Upvotes: 1
Reputation: 66
Cleaning the project solved my problem.
Steps: Product -> Clean(or Shift + Cmd + K)
Upvotes: 2
Reputation: 61
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.
Upvotes: 1
Reputation: 2589
if you are accessing it from different module
or Target
then you just need it to public
it
Upvotes: 1
Reputation: 14261
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.
Also make sure the file is part of the framework target. (The little box in the screenshot below should be checked)
Upvotes: 14
Reputation: 189
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
UTF-8 Screenshot
Upvotes: 2
Reputation: 20610
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.
Upvotes: 1
Reputation: 31
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
Upvotes: 3
Reputation: 21254
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.
Upvotes: 2
Reputation: 41
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])
Upvotes: 4
Reputation: 1081
as others mentioned well and in this thread
use of unneeded swift files in "copy bundle resources"
Upvotes: 1
Reputation: 5617
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!
Upvotes: 2
Reputation: 6812
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. :)
Upvotes: 3
Reputation: 1894
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)
Upvotes: 0
Reputation: 14633
I tried many of the solutions offered here, but eventually deleted the file and created it again, and Xcode was mollified :/
Upvotes: 4
Reputation: 605
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
Upvotes: 1
Reputation: 941
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
Upvotes: 3
Reputation: 3802
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.
To see the target membership. Select your file then open the file inspector (⌥ + ⌘ + 1) [option] + [command] + 1
Upvotes: 190
Reputation: 7719
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.
Upvotes: 12
Reputation: 2431
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.
Upvotes: 28
Reputation: 9649
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".
Upvotes: 2
Reputation: 29
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.
Upvotes: 2
Reputation: 943
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.
Upvotes: 5