PC パソコン

How to Fix Swift “Cannot Find Type in Scope” in Xcode

When building Swift code in Xcode, you may encounter an error such as:

Cannot find type 'SomeType' in scope

For example:

var user: UserProfile // Cannot find type 'UserProfile' in scope

This error means that the Swift compiler cannot resolve the requested type from the current scope.

The cause may be as simple as a typo, but it can also be a missing import, incorrect Target Membership, a source file excluded from Compile Sources, a non-public type in another module, a missing Swift Package product, conditional compilation, or generated code that was never produced.

The most useful first step is to determine exactly where the missing type is defined and whether the current file, target, and module are allowed to access it.

  1. What does “Cannot find type in scope” mean?
  2. Common causes
    1. 1. The type name is misspelled
    2. 2. The capitalization is wrong
    3. 3. The required module is not imported
    4. 4. The source file is not included in Target Membership
    5. 5. The file is missing from Compile Sources
    6. 6. The type is internal to another module
    7. 7. A nested type is referenced incorrectly
    8. 8. A type alias is unavailable
    9. 9. A Swift Package product is not attached to the target
    10. 10. A framework is not attached to the current target
    11. 11. A CocoaPods project is opened through the .xcodeproj
    12. 12. Conditional compilation removes the type
    13. 13. The type exists only on another platform
    14. 14. Availability differs by OS or SDK
    15. 15. Generated code was never generated
    16. 16. A generated file is not compiled by the target
    17. 17. A test target cannot see an app type
    18. 18. An old type name remains after a refactor
    19. 19. A generic type parameter is outside its scope
    20. 20. An extension targets a type from an unimported module
  3. Typical situations where the error appears
  4. What to check first
    1. 1. Search for the type declaration
    2. 2. Identify the module that owns the type
  5. Solution 1: Fix the type name
  6. Solution 2: Import the required module
  7. Solution 3: Check Target Membership
  8. Solution 4: Check Compile Sources
  9. Solution 5: Make cross-module types public
  10. Solution 6: Use the correct nested type name
  11. Solution 7: Attach the Swift Package product to the target
  12. Solution 8: Check framework integration
  13. Solution 9: Open the CocoaPods workspace
  14. Solution 10: Check conditional compilation
  15. Solution 11: Check platform conditions
  16. Solution 12: Check active compilation conditions
  17. Solution 13: Verify generated code exists
  18. Solution 14: Compile the generated file
  19. Solution 15: Find stale references after renaming a type
  20. Solution 16: Use @testable import when appropriate
  21. Solution 17: Declare generic parameters in the correct scope
  22. Solution 18: Import the module before extending its type
  23. Solution 19: Check framework access control
  24. Solution 20: Qualify identical type names with module names
  25. Solution 21: Compare Debug and Release compilation conditions
  26. Solution 22: Resolve Swift Package dependencies again
  27. Solution 23: Reset package caches
  28. Solution 24: Clean the build folder
  29. Solution 25: Remove DerivedData if necessary
  30. If your own struct cannot be found
  31. If a framework type cannot be found
  32. If a Swift Package type cannot be found
  33. If the error occurs only in tests
  34. If Debug works but Release fails
  35. If iOS works but another platform fails
  36. “Cannot find type in scope” versus “Cannot find in scope”
  37. What you should avoid
  38. Fast troubleshooting checklist
  39. Summary

What does “Cannot find type in scope” mean?

Swift classes, structs, enums, protocols, and type aliases are all types.

For example:

struct UserProfile { let name: String }

var user: UserProfile

This works because UserProfile is visible from the current scope.

If the compiler cannot resolve that type, it reports:

Cannot find type 'UserProfile' in scope

Common causes

1. The type name is misspelled

struct UserProfile { }

var user: UserProfie

UserProfile and UserProfie are different identifiers.

2. The capitalization is wrong

Swift identifiers are case-sensitive.

struct UserProfile { }

var user: userProfile

3. The required module is not imported

If the type comes from another framework or package, import the module that provides it.

import SomeFramework

var object: SomeFrameworkType

4. The source file is not included in Target Membership

A custom type can exist in another Swift file but still be invisible to the current target if that file is not part of the target.

5. The file is missing from Compile Sources

Project changes or merges can leave an implementation file outside the target’s compile phase.

6. The type is internal to another module

Swift declarations are internal by default.

A type that must be used from another framework or module may need to be public:

public struct UserProfile { public let name: String
public init(name: String) {
    self.name = name
}

}

7. A nested type is referenced incorrectly

If the type is nested:

struct API { struct Response { } }

reference it as:

let response: API.Response

8. A type alias is unavailable

The type alias itself may live in another file or module that is not visible.

typealias UserID = String

9. A Swift Package product is not attached to the target

The package may exist in the project while its product is not a dependency of the affected target.

10. A framework is not attached to the current target

The framework that defines the type may not be linked or otherwise available to the target.

11. A CocoaPods project is opened through the .xcodeproj

When CocoaPods integration depends on the workspace, opening only the .xcodeproj can make Pod modules unavailable.

12. Conditional compilation removes the type

#if DEBUG struct DebugUser { } #endif

DebugUser does not exist in a Release build.

13. The type exists only on another platform

#if os(iOS) struct IOSOnlyType { } #endif

This type is not compiled for macOS.

14. Availability differs by OS or SDK

A newer SDK type may not be available in every deployment or compiler context.

15. Generated code was never generated

Tools such as SwiftGen, Sourcery, or OpenAPI generators may be expected to create the missing type.

16. A generated file is not compiled by the target

The generated file may exist but still not be included in the target’s source compilation.

17. A test target cannot see an app type

Tests may need to import the application module or use @testable import when accessing internal declarations.

18. An old type name remains after a refactor

The declaration may have been renamed while references still use the previous name.

19. A generic type parameter is outside its scope

Incorrect:

func makeValue() -> T { fatalError() }

T was never declared.

20. An extension targets a type from an unimported module

If an extension references an external type, the module that defines that type must be visible.

Typical situations where the error appears

  • After creating a struct or class in a new file
  • After moving source files between targets
  • After changing Target Membership
  • After adding a Swift Package
  • After installing CocoaPods
  • After moving a type into a framework
  • After changing framework access control
  • After renaming a type
  • When Debug works but Release fails
  • When iOS works but another platform target fails
  • After introducing generated source code
  • After adding unit tests

What to check first

1. Search for the type declaration

Use Xcode’s Find Navigator and search for:

UserProfile

If no declaration exists in the project, determine whether the type is supposed to come from an external dependency.

2. Identify the module that owns the type

Determine whether it belongs to your app, an Apple framework, your own framework, or a third-party dependency.

Solution 1: Fix the type name

// Declaration struct UserProfile { }

// Incorrect
var user: UserProfie

// Correct
var user: UserProfile

Solution 2: Import the required module

import UIKit import Combine import SomeFramework

Check the dependency’s documentation for the module that provides the type.

Solution 3: Check Target Membership

Select the Swift file that defines the missing type and inspect:

File Inspector → Target Membership → Enable the affected target

Solution 4: Check Compile Sources

Open:

Target → Build Phases → Compile Sources

Make sure the Swift file containing the type is included.

Solution 5: Make cross-module types public

Framework:

public struct UserProfile { public let name: String
public init(name: String) {
    self.name = name
}

}

Consumer:

import UserKit

let user = UserProfile(name: "Alice")

Important: Making only the type public may not be enough. Initializers and properties that clients need must also have appropriate access levels.

Solution 6: Use the correct nested type name

struct API { struct Response { let status: Int } }

let response: API.Response

Solution 7: Attach the Swift Package product to the target

For Package.swift:

.target( name: "MyApp", dependencies: [ .product( name: "SomeLibrary", package: "SomePackage" ) ] )

For an Xcode project, inspect:

Target → General → Frameworks, Libraries, and Embedded Content

Solution 8: Check framework integration

Make sure the framework that defines the type belongs to the affected target.

Target → General → Frameworks, Libraries, and Embedded Content

Solution 9: Open the CocoaPods workspace

Run:

pod install

Then open:

MyApp.xcworkspace

Solution 10: Check conditional compilation

If the type is declared as:

#if DEBUG struct DebugSettings { } #endif

it cannot be referenced in Release unless the condition is changed.

Solution 11: Check platform conditions

#if os(iOS) struct DeviceInfo { } #endif

Do not reference DeviceInfo from a platform where it is not compiled.

Solution 12: Check active compilation conditions

Inspect:

SWIFT_ACTIVE_COMPILATION_CONDITIONS

From Terminal:

xcodebuild -showBuildSettings | grep SWIFT_ACTIVE_COMPILATION_CONDITIONS

If your source uses:

#if FEATURE_A struct FeatureAType { } #endif

make sure FEATURE_A is actually enabled for the current configuration.

Solution 13: Verify generated code exists

For generated Swift files:

find . -name "Generated*.swift"

If nothing is generated, inspect the code-generation build phase.

Solution 14: Compile the generated file

If the generated file exists in the project:

Target → Build Phases → Compile Sources

Make sure the file is part of the target.

Solution 15: Find stale references after renaming a type

If:

OldUserProfile ↓ UserProfile

search remaining source references:

grep -R "OldUserProfile" . \ --include="*.swift"

Solution 16: Use @testable import when appropriate

A unit-test target that needs internal declarations from the app module may use:

@testable import MyApp

Note: @testable import is intended for tests. It should not be used as a workaround for inappropriate production-module access control.

Solution 17: Declare generic parameters in the correct scope

Incorrect:

func makeValue() -> T { fatalError() }

Correct:

func makeValue<T>() -> T { fatalError() }

Solution 18: Import the module before extending its type

import SomeFramework

extension SomeExternalType {
...
}

Solution 19: Check framework access control

This declaration:

struct InternalModel { }

is internal by default.

For external clients:

public struct PublicModel { public init() {} }

Solution 20: Qualify identical type names with module names

If two modules contain the same type name:

let model: ModuleA.User let other: ModuleB.User

Explicit qualification can clarify which type you intend to use.

Solution 21: Compare Debug and Release compilation conditions

xcodebuild -showBuildSettings \ -configuration Debug | grep SWIFT_ACTIVE_COMPILATION_CONDITIONS

xcodebuild -showBuildSettings
-configuration Release | grep SWIFT_ACTIVE_COMPILATION_CONDITIONS

Look for types that are compiled only in one configuration.

Solution 22: Resolve Swift Package dependencies again

For a project:

xcodebuild -resolvePackageDependencies \ -project MyApp.xcodeproj

For a workspace:

xcodebuild -resolvePackageDependencies \ -workspace MyApp.xcworkspace \ -scheme MyApp

Solution 23: Reset package caches

If the error started after updating a package, reset Xcode’s package caches and resolve again.

For a command-line Swift package:

swift package reset swift package resolve

Solution 24: Clean the build folder

After fixing the dependency or target configuration:

Product → Clean Build Folder

Solution 25: Remove DerivedData if necessary

rm -rf ~/Library/Developer/Xcode/DerivedData/*

Important: DerivedData cleanup cannot create a missing type, fix Target Membership, or change access control. Use it only after fixing the real configuration problem.

If your own struct cannot be found

If you created:

struct UserProfile { }

but another file reports:

Cannot find type 'UserProfile' in scope

check:

  1. UserProfile.swift belongs to the target.
  2. It appears in Compile Sources.
  3. Conditional compilation is not excluding it.
  4. The type name and capitalization match exactly.

If a framework type cannot be found

Check:

  1. The framework module is imported.
  2. The framework belongs to the affected target.
  3. The type is public.
  4. Required initializers and members are public.
  5. The installed framework version actually contains the type.

If a Swift Package type cannot be found

Check:

  1. The package resolved successfully.
  2. The required product is attached to the target.
  3. The correct module is imported.
  4. The installed package version contains the type.
  5. The package cache is not stale.

If the error occurs only in tests

A test target does not automatically share the application’s source scope.

When appropriate, use:

@testable import MyApp

and verify test target dependencies and host application settings.

If Debug works but Release fails

Look for declarations such as:

#if DEBUG struct DebugModel { } #endif

Make sure production code does not depend on a Debug-only type.

If iOS works but another platform fails

For example:

#if os(iOS) typealias PlatformView = UIView #endif

PlatformView does not exist in a macOS build.

Provide appropriate definitions for each supported platform.

“Cannot find type in scope” versus “Cannot find in scope”

“Cannot find type in scope” is used when Swift expects a type name, such as:

var item: UserProfile

When a variable, function, or other value identifier is missing:

print(currentUser)

Swift typically reports:

Cannot find 'currentUser' in scope

What you should avoid

Do not treat DerivedData deletion as the primary fix for “Cannot find type in scope.”

The error usually means that the declaration is genuinely not visible to the current target or module.

Also avoid making every declaration public just to make the error disappear. Expose only the API that is intentionally part of the framework’s public interface.

Fast troubleshooting checklist

  1. Check the type name and capitalization.
  2. Search for the type declaration.
  3. Import the module that defines it.
  4. Check Target Membership.
  5. Check Compile Sources.
  6. For another module, verify access control.
  7. For nested types, use the correct qualified name.
  8. For SwiftPM, attach the product to the target.
  9. For CocoaPods, open the workspace.
  10. Check conditional compilation and platform conditions.
  11. Verify generated source files exist.
  12. For tests, review @testable import.
  13. Clean package caches and DerivedData only after fixing the configuration.

Summary

Swift’s “Cannot find type in scope” error means that the compiler cannot resolve the requested type from the current scope.

Common causes include misspelled type names, missing module imports, incorrect Target Membership, source files missing from Compile Sources, cross-module access-control restrictions, missing Swift Package or CocoaPods dependencies, conditional compilation, and missing generated source code.

The most effective troubleshooting method is to identify exactly where the type is defined and then verify that the current file, target, and module can actually access that declaration.

For your own types, focus on Target Membership and Compile Sources. For framework types, check imports and access control. For Swift Packages, verify product dependencies. If the error differs between Debug and Release, inspect compilation conditions before clearing caches.