All posts

Fix Missing Import in Swift

Fix Swift import errors, including why Foundation makes exit(0) available on Apple platforms, plus SPM dependencies and framework linking.

Missing Import Errors in Swift

Swift's No such module 'X' error means the compiler can't find the module. This is usually a dependency configuration issue, not a missing import statement.

Why `import Foundation` Makes `exit(0)` Available

The exit(_:) function belongs to the platform C library, not Foundation. On Apple platforms, importing Foundation currently re-exports Darwin APIs, so this compiles:

import Foundation
exit(0)

Do not rely on that indirect import for cross-platform command-line tools. Import the module that owns exit explicitly:

#if canImport(Darwin)
import Darwin
#elseif canImport(Glibc)
import Glibc
#endif

exit(EXIT_SUCCESS)

For a successful natural return from main.swift, you can usually omit exit(0). Calling exit terminates immediately and skips pending defer blocks and object cleanup.

Swift Package Manager

// Package.swift
let package = Package(
    name: "MyApp",
    dependencies: [
        .package(url: "https://github.com/Alamofire/Alamofire", from: "5.8.0"),
    ],
    targets: [
        .target(
            name: "MyApp",
            dependencies: ["Alamofire"]),  // Must list here too!
    ]
)
swift package resolve
swift build

Xcode: Framework Not Linked

In Xcode projects:

  1. Select your target → General → Frameworks, Libraries, and Embedded Content
  2. Add the missing framework
  3. Clean build folder: Cmd+Shift+K

CocoaPods

# Podfile
pod 'Alamofire', '~> 5.8'
pod install
# Open .xcworkspace, NOT .xcodeproj

Common Swift Imports

import Foundation     // Strings, dates, networking basics
import UIKit          // iOS UI (not available on macOS)
import SwiftUI        // Declarative UI
import Combine        // Reactive programming
import CoreData       // Persistence

Bridging Header for Objective-C

If importing an ObjC library:

// MyApp-Bridging-Header.h
#import <SomeObjCLib/SomeObjCLib.h>

Then use it directly in Swift without an import statement.

Bugsly's iOS SDK captures runtime crashes with symbolicated stack traces, including module load failures that might not surface until a specific code path is executed.

Try Bugsly Free

Track up to 100 issues per month on the free plan, with unlimited events and no credit card required.

Get Started Free