简体   繁体   中英

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

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?

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.


目标会员

Edit

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

详细说明

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.

截图 1

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

截图 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), eg:

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

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

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

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.

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 ?

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

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

我尝试了这里提供的许多解决方案,但最终删除了该文件并重新创建了它,并且 Xcode 被缓和了:/

如果您不小心将参数名称大写,并将其命名为与对象相同的名称,也会发生这种情况。

class func didRecieveData(BlockItems: [BlockItems])

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

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

我在 Xcode 8 中将代码重构到框架中时收到此错误消息,结果是我忘记将框架中的类声明为public

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.

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

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!

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.

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

在此处输入图片说明

Cleaning the project solved my problem.

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

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.

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

as others mentioned well and in this thread

use of unneeded swift files in "copy bundle resources"帮助的屏幕截图

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.

如果您从不同的moduleTarget访问它,那么您只需要将其public

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.

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

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.

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM