PC パソコン

How to Fix “Use of Unresolved Identifier” in Xcode

When building Swift code in Xcode, you may encounter:

Use of unresolved identifier 'someName'

For example:

print(userName) // Use of unresolved identifier 'userName'

This error means that the Swift compiler cannot resolve the specified identifier from the current code context.

In newer Swift toolchains, closely related problems are often reported as Cannot find '...' in scope. The older “Use of unresolved identifier” wording is especially common in older Xcode and Swift versions, but the underlying troubleshooting process is largely the same.

Typical causes include misspelled variable or function names, references outside the declaration’s scope, missing module imports, incorrect Target Membership, missing framework or Swift Package dependencies, and declarations excluded by conditional compilation.

The best starting point is to determine where the unresolved identifier is supposed to be declared.

  1. What does “Use of unresolved identifier” mean?
  2. How is this related to “Cannot find in scope”?
  3. Common causes
    1. 1. The identifier is misspelled
    2. 2. Capitalization is different
    3. 3. The declaration is outside the current scope
    4. 4. A function-local variable is used outside the function
    5. 5. A loop variable is used outside the loop
    6. 6. An if-let binding is used outside its scope
    7. 7. A closure parameter is used outside the closure
    8. 8. A function was renamed
    9. 9. A property was renamed
    10. 10. A property must be accessed through its instance
    11. 11. The required module is not imported
    12. 12. The source file is not part of the target
    13. 13. The source file is missing from Compile Sources
    14. 14. A declaration in another module is internal
    15. 15. A Swift Package product is not attached to the target
    16. 16. A CocoaPods project is opened through .xcodeproj
    17. 17. The declaration exists only in DEBUG
    18. 18. A custom compilation condition is missing
    19. 19. A platform condition excludes the declaration
    20. 20. Generated code was never created
    21. 21. An IBOutlet or IBAction was renamed
    22. 22. A SwiftUI property wrapper was renamed
    23. 23. A generic parameter is outside its scope
    24. 24. The module for an extension target type is not imported
    25. 25. The code uses an obsolete Swift or SDK API
  4. Typical situations where the error appears
  5. What to check first
    1. 1. Search for the unresolved identifier
    2. 2. Determine the declaration’s scope
  6. Solution 1: Fix spelling and capitalization
  7. Solution 2: Fix the variable scope
  8. Solution 3: Return local values from functions
  9. Solution 4: Use a property for shared state
  10. Solution 5: Access properties through their instance
  11. Solution 6: Use self when needed
  12. Solution 7: Search for stale names after refactoring
  13. Solution 8: Import the required module
  14. Solution 9: Check Target Membership
  15. Solution 10: Check Compile Sources
  16. Solution 11: Check cross-module access control
  17. Solution 12: Attach the Swift Package product
  18. Solution 13: Resolve Swift Package dependencies again
  19. Solution 14: Open the CocoaPods workspace
  20. Solution 15: Review DEBUG-only declarations
  21. Solution 16: Check Active Compilation Conditions
  22. Solution 17: Review platform conditions
  23. Solution 18: Keep if-let bindings within scope
  24. Solution 19: Do not use loop variables outside the loop
  25. Solution 20: Keep closure parameters inside the closure
  26. Solution 21: Keep switch bindings within their cases
  27. Solution 22: Verify generated code exists
  28. Solution 23: Check SwiftUI property names
  29. Solution 24: Update projected-value references after renaming properties
  30. Solution 25: Check imports in test targets
  31. Solution 26: Verify the API in the current SDK
  32. Solution 27: Use Xcode Refactor → Rename
  33. Solution 28: Compare Debug and Release
  34. Solution 29: Clean the build folder
  35. Solution 30: Remove DerivedData if necessary
  36. If the unresolved identifier is a variable
  37. If the unresolved identifier is a function
  38. If the unresolved identifier is a type
  39. If the error occurs in SwiftUI
  40. If the app target works but an extension fails
  41. If the error occurs only in Release
  42. If the build works locally but fails in CI
  43. What you should avoid
  44. Fast troubleshooting checklist
  45. Summary

What does “Use of unresolved identifier” mean?

An identifier is a name used by your Swift program.

Examples include:

  • Variables
  • Constants
  • Functions
  • Properties
  • Methods
  • Classes
  • Structs
  • Enums
  • Protocols

This code works:

let userName = "Alice" print(userName)

because userName has been declared.

If the code instead contains only:

print(userName)

and no visible declaration exists, the compiler cannot resolve the identifier.

How is this related to “Cannot find in scope”?

Modern Swift compilers often report a similar problem as:

Cannot find 'userName' in scope

So if an older tutorial or project reports:

Use of unresolved identifier

while your current Xcode reports:

Cannot find ... in scope

you should initially troubleshoot both as name-resolution problems.

Common causes

1. The identifier is misspelled

let userName = "Alice"

print(userNmae)

userName and userNmae are different identifiers.

2. Capitalization is different

Swift identifiers are case-sensitive.

let currentUser = "Alice"

print(CurrentUser)

3. The declaration is outside the current scope

if isLoggedIn { let message = "Welcome" }

print(message)

message is visible only inside the if block.

4. A function-local variable is used outside the function

func loadUser() { let user = "Alice" }

print(user)

5. A loop variable is used outside the loop

for item in items { print(item) }

print(item)

6. An if-let binding is used outside its scope

if let user = currentUser { print(user.name) }

print(user.name)

7. A closure parameter is used outside the closure

users.forEach { user in print(user.name) }

print(user)

8. A function was renamed

func fetchUsers() { }

// Old call
loadUsers()

9. A property was renamed

Old references may remain after refactoring.

10. A property must be accessed through its instance

struct User { let name: String }

let user = User(name: "Alice")

print(user.name)

11. The required module is not imported

import UIKit import Combine import SomeFramework

12. The source file is not part of the target

A declaration in another Swift file is unavailable if that file does not belong to the current target.

13. The source file is missing from Compile Sources

This can happen after target changes or project-file merges.

14. A declaration in another module is internal

Swift declarations are generally internal by default.

An API intended for clients in another module may need:

public func fetchUsers() { }

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

The package may exist in the project while the product containing the API is not linked to the affected target.

16. A CocoaPods project is opened through .xcodeproj

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

17. The declaration exists only in DEBUG

#if DEBUG let apiURL = "https://debug.example.com" #endif

apiURL does not exist in Release.

18. A custom compilation condition is missing

#if FEATURE_CAMERA func openCamera() { } #endif

19. A platform condition excludes the declaration

#if os(iOS) func openSettings() { } #endif

The function does not exist in a macOS target.

20. Generated code was never created

If SwiftGen, Sourcery, OpenAPI Generator, or another tool is expected to create the identifier, a failed generation step leaves nothing for the compiler to resolve.

21. An IBOutlet or IBAction was renamed

Refactoring UIKit code can leave obsolete references to methods or properties.

22. A SwiftUI property wrapper was renamed

@State private var userName = ""

var body: some View {
Text(username)
}

The correct identifier is userName.

23. A generic parameter is outside its scope

func convert<T>(_ value: T) { }

let value: T

T does not exist outside the generic declaration context.

24. The module for an extension target type is not imported

extension SomeExternalType { }

The module defining SomeExternalType must be available.

25. The code uses an obsolete Swift or SDK API

Old tutorials may reference APIs that have since been renamed or removed.

In that case, verify the current SDK or library documentation rather than assuming the problem is only lexical scope.

Typical situations where the error appears

  • After renaming a variable
  • After renaming a function
  • After moving code outside a function or closure
  • After moving a source file to another target
  • After changing Target Membership
  • After moving code into a framework
  • After adding a Swift Package
  • After adding CocoaPods
  • When Debug succeeds but Release fails
  • When iOS succeeds but another platform fails
  • When generated source code is missing
  • When using old Swift sample code in a modern Xcode project

What to check first

1. Search for the unresolved identifier

If Xcode reports:

Use of unresolved identifier 'loadUsers'

search the project for:

loadUsers

Verify that the declaration still exists and was not renamed.

2. Determine the declaration’s scope

Check whether it is declared inside a:

  • Function
  • Conditional block
  • Loop
  • Closure
  • Type
  • Different file
  • Different target
  • Different module

Solution 1: Fix spelling and capitalization

let currentUser = "Alice"

// Incorrect
print(currentuser)

// Correct
print(currentUser)

Solution 2: Fix the variable scope

Incorrect:

if isLoggedIn { let message = "Welcome" }

print(message)

If the value truly needs a wider scope:

var message = ""

if isLoggedIn {
message = "Welcome"
}

print(message)

Note: Do not broaden scope unnecessarily. Keep values visible only for as long and as widely as the design requires.

Solution 3: Return local values from functions

func loadUserName() -> String { let userName = "Alice" return userName }

let userName = loadUserName()
print(userName)

Solution 4: Use a property for shared state

final class UserManager { var currentUser: String?
func login() {
    currentUser = "Alice"
}

func logout() {
    currentUser = nil
}

}

Solution 5: Access properties through their instance

struct User { let name: String }

let user = User(name: "Alice")

print(user.name)

Solution 6: Use self when needed

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

}

Solution 7: Search for stale names after refactoring

If:

loadUsers() ↓ fetchUsers()

search for remaining old references:

grep -R "loadUsers" . --include="*.swift"

Solution 8: Import the required module

import Foundation import UIKit import Combine import SomeFramework

Solution 9: Check Target Membership

Select the source file that contains the declaration:

File Inspector → Target Membership → Enable the affected target

Solution 10: Check Compile Sources

Target → Build Phases → Compile Sources

Verify that the source file containing the declaration is compiled by the affected target.

Solution 11: Check cross-module access control

Framework:

public func fetchUsers() { }

Application:

import UserKit

fetchUsers()

Expose only declarations that are intended to be part of the module’s public API.

Solution 12: Attach the Swift Package product

For Package.swift:

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

For an Xcode project, also check:

Target → General → Frameworks, Libraries, and Embedded Content

Solution 13: Resolve Swift Package dependencies again

For a project:

xcodebuild -resolvePackageDependencies \ -project MyApp.xcodeproj

For a workspace:

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

Solution 14: Open the CocoaPods workspace

Run:

pod install

Then open:

MyApp.xcworkspace

Solution 15: Review DEBUG-only declarations

#if DEBUG func debugLog() { print("debug") } #endif

Do not call debugLog() from Release code unless an equivalent declaration exists there.

Solution 16: Check Active Compilation Conditions

xcodebuild -showBuildSettings | grep SWIFT_ACTIVE_COMPILATION_CONDITIONS

If your code uses:

#if FEATURE_CAMERA func openCamera() { } #endif

make sure FEATURE_CAMERA is enabled for the active configuration.

Solution 17: Review platform conditions

#if os(iOS) func platformAction() { // iOS } #elseif os(macOS) func platformAction() { // macOS } #endif

Provide implementations for every supported platform that references the API.

Solution 18: Keep if-let bindings within scope

Incorrect:

if let user = currentUser { print(user.name) }

update(user)

Correct:

if let user = currentUser { print(user.name) update(user) }

Solution 19: Do not use loop variables outside the loop

var selectedItem: Item?

for item in items {
if item.isSelected {
selectedItem = item
}
}

if let selectedItem {
print(selectedItem)
}

Solution 20: Keep closure parameters inside the closure

Incorrect:

users.map { user in user.name }

print(user)

Capture the returned value instead:

let names = users.map { user in user.name }

print(names)

Solution 21: Keep switch bindings within their cases

switch result { case .success(let data): process(data)

case .failure(let error):
print(error)
}

Solution 22: Verify generated code exists

find . -name "Generated*.swift"

If generated source is missing, check:

  • The generation script completed successfully.
  • Required command-line tools are installed.
  • The output path is correct.
  • The generated file is compiled by the target.

Solution 23: Check SwiftUI property names

Incorrect:

struct ContentView: View { @State private var userName = ""
var body: some View {
    Text(username)
}

}

Correct:

Text(userName)

Solution 24: Update projected-value references after renaming properties

If:

@Binding var name: String

is renamed, references such as:

$name

must also be updated.

Solution 25: Check imports in test targets

Unit tests that need internal declarations from an application module may use:

@testable import MyApp

Note: @testable import is intended for testing. It should not be used to bypass production access-control design.

Solution 26: Verify the API in the current SDK

If the code comes from an old tutorial, confirm that the identifier still exists in the current SDK or library version.

An API may have been renamed, moved to a type method, replaced, or removed entirely.

Solution 27: Use Xcode Refactor → Rename

When renaming variables, functions, properties, or types, Xcode’s refactoring support can update many compile-time references automatically.

Still review dynamic strings, selectors, and other references that cannot be tracked statically.

Solution 28: Compare Debug and Release

xcodebuild -showBuildSettings \ -configuration Debug | grep SWIFT_ACTIVE_COMPILATION_CONDITIONS

xcodebuild -showBuildSettings
-configuration Release | grep SWIFT_ACTIVE_COMPILATION_CONDITIONS

Look for declarations that exist only in one configuration.

Solution 29: Clean the build folder

After correcting the source or target configuration:

Product → Clean Build Folder

Solution 30: Remove DerivedData if necessary

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

Important: Deleting DerivedData cannot fix a genuinely missing identifier, an out-of-scope local variable, incorrect Target Membership, or a missing import. Use it only after fixing the actual source or project problem.

If the unresolved identifier is a variable

Check:

  1. Whether the variable is declared.
  2. Whether spelling and capitalization match.
  3. Whether it is outside the current scope.
  4. Whether it belongs to an instance.
  5. Whether conditional compilation removes it.

If the unresolved identifier is a function

For:

Use of unresolved identifier 'fetchUsers'

check:

  1. The function still exists.
  2. It was not renamed.
  3. It is not an instance method that needs an object.
  4. Its source file belongs to the target.
  5. The defining module is imported.
  6. It is public when used across modules.

If the unresolved identifier is a type

Older Swift compilers may report a missing type as:

Use of unresolved identifier 'UserProfile'

Modern Swift may instead report:

Cannot find type 'UserProfile' in scope

In that case, check module imports, Target Membership, access control, and Swift Package products.

If the error occurs in SwiftUI

Review:

  • @State property names
  • @Binding property names
  • @Environment declarations
  • @EnvironmentObject declarations
  • Local values inside view-builder scopes
  • Preview-only declarations behind compilation conditions

If the app target works but an extension fails

Widgets, Share Extensions, Notification Service Extensions, and other extensions are separate targets.

A Swift file included in the app target may not automatically belong to the extension target.

Check:

File Inspector → Target Membership

For larger shared codebases, consider placing common code in a shared framework or Swift Package.

If the error occurs only in Release

Inspect:

#if DEBUG

and custom compilation conditions.

Make sure identifiers required by production code are not declared only in Debug builds.

If the build works locally but fails in CI

Check:

  • Required source files are committed to Git.
  • Generated code is produced in CI.
  • Swift Package versions match.
  • CocoaPods dependencies are installed consistently.
  • Compilation conditions match local settings.
  • The CI Xcode version is compatible with the project.

What you should avoid

Do not repeatedly delete DerivedData as the first response to “Use of unresolved identifier.”

In most cases, the Swift compiler genuinely cannot see the declaration from the current scope.

Also avoid converting local variables into global variables just to make them visible.

Use function parameters, return values, instance properties, or another structure that matches the lifetime and ownership of the data.

Fast troubleshooting checklist

  1. Check spelling and capitalization.
  2. Search the project for the declaration.
  3. Compare declaration scope with the use site.
  4. Check local variables in functions, loops, conditionals, and closures.
  5. Search for stale names after refactoring.
  6. Import the required module.
  7. Check Target Membership and Compile Sources.
  8. Check access control across modules.
  9. Verify Swift Package products.
  10. For CocoaPods, open the workspace.
  11. Review DEBUG and custom compilation conditions.
  12. Verify generated code exists.
  13. Confirm the API still exists in the current SDK.
  14. Clean build caches only after fixing the real problem.

Summary

“Use of unresolved identifier” means that the Swift compiler cannot resolve a requested identifier from the current scope.

The wording is especially common in older Swift and Xcode releases, while modern Swift often reports similar problems as “Cannot find ‘…’ in scope.”

Common causes include misspellings, references outside local scope, stale names after refactoring, missing module imports, incorrect Target Membership, cross-module access control, missing Swift Package or CocoaPods dependencies, conditional compilation, and missing generated code.

The most effective troubleshooting question is: where is this identifier supposed to be declared?

Start with lexical scope, then check the source file, target, and module. Treat cache cleanup as a final supporting step rather than the primary fix.