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.
- What does “Cannot find type in scope” mean?
- Common causes
- 1. The type name is misspelled
- 2. The capitalization is wrong
- 3. The required module is not imported
- 4. The source file is not included in Target Membership
- 5. The file is missing from Compile Sources
- 6. The type is internal to another module
- 7. A nested type is referenced incorrectly
- 8. A type alias is unavailable
- 9. A Swift Package product is not attached to the target
- 10. A framework is not attached to the current target
- 11. A CocoaPods project is opened through the .xcodeproj
- 12. Conditional compilation removes the type
- 13. The type exists only on another platform
- 14. Availability differs by OS or SDK
- 15. Generated code was never generated
- 16. A generated file is not compiled by the target
- 17. A test target cannot see an app type
- 18. An old type name remains after a refactor
- 19. A generic type parameter is outside its scope
- 20. An extension targets a type from an unimported module
- Typical situations where the error appears
- What to check first
- Solution 1: Fix the type name
- Solution 2: Import the required module
- Solution 3: Check Target Membership
- Solution 4: Check Compile Sources
- Solution 5: Make cross-module types public
- Solution 6: Use the correct nested type name
- Solution 7: Attach the Swift Package product to the target
- Solution 8: Check framework integration
- Solution 9: Open the CocoaPods workspace
- Solution 10: Check conditional compilation
- Solution 11: Check platform conditions
- Solution 12: Check active compilation conditions
- Solution 13: Verify generated code exists
- Solution 14: Compile the generated file
- Solution 15: Find stale references after renaming a type
- Solution 16: Use @testable import when appropriate
- Solution 17: Declare generic parameters in the correct scope
- Solution 18: Import the module before extending its type
- Solution 19: Check framework access control
- Solution 20: Qualify identical type names with module names
- Solution 21: Compare Debug and Release compilation conditions
- Solution 22: Resolve Swift Package dependencies again
- Solution 23: Reset package caches
- Solution 24: Clean the build folder
- Solution 25: Remove DerivedData if necessary
- If your own struct cannot be found
- If a framework type cannot be found
- If a Swift Package type cannot be found
- If the error occurs only in tests
- If Debug works but Release fails
- If iOS works but another platform fails
- “Cannot find type in scope” versus “Cannot find in scope”
- What you should avoid
- Fast troubleshooting checklist
- 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:
- UserProfile.swift belongs to the target.
- It appears in Compile Sources.
- Conditional compilation is not excluding it.
- The type name and capitalization match exactly.
If a framework type cannot be found
Check:
- The framework module is imported.
- The framework belongs to the affected target.
- The type is public.
- Required initializers and members are public.
- The installed framework version actually contains the type.
If a Swift Package type cannot be found
Check:
- The package resolved successfully.
- The required product is attached to the target.
- The correct module is imported.
- The installed package version contains the type.
- 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
- Check the type name and capitalization.
- Search for the type declaration.
- Import the module that defines it.
- Check Target Membership.
- Check Compile Sources.
- For another module, verify access control.
- For nested types, use the correct qualified name.
- For SwiftPM, attach the product to the target.
- For CocoaPods, open the workspace.
- Check conditional compilation and platform conditions.
- Verify generated source files exist.
- For tests, review @testable import.
- 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.
