Sample CodemacOSReviewed 2026-07-21View on Apple Developer

Calling APIs Across Language Boundaries

At a glance

Item Summary
Purpose Use a variety of C++ APIs in Swift – and vice-versa – across multiple targets and frameworks in an Xcode project.
App architecture A C++, C/Objective-C header, Objective-C++, Swift sample with the source-visible chain SwiftCppInteroperabilityShowcaseAppContentViewSwiftUI APIs.
Main patterns No named application pattern supported by the extracted structure
Project style 13 scanned source file(s) across C++, C/Objective-C header, Objective-C++, Swift, organized around ranked entry, type, and file boundaries.

Project structure

Source bundle/
├── Swift-and-Cplusplus-InteroperabilityShowcase/
│   ├── SwiftCppInteroperabilityShowcaseApp.swift
│   ├── ContentView.swift
│   └── EnchantedForest.swift
├── ForestUI/
│   ├── ForestView.swift
│   └── Tree+UI.swift
├── ForestBuilder/
│   ├── ForestBuilder.h
│   ├── MagicForestBuilder.mm
│   └── MagicForest.swift
├── Forest/
│   ├── Forest.cpp
│   ├── Forest.h
│   └── Tree.cpp
└── ForestTestSupport/
    └── Tree+Equatable.swift

Structure observations

  • Architecturally prominent files are ranked from entry points and role-named declarations; resource-only paths are omitted.
  • Primary languages: C++, C/Objective-C header, Objective-C++, Swift.
  • The verified tree contains 4 project/configuration file(s) and 6 source declaration(s).

Overall architecture

Reference code

Swift-and-Cplusplus-InteroperabilityShowcase/SwiftCppInteroperabilityShowcaseApp.swift:10 — architecture anchor

@main
struct SwiftCppInteroperabilityShowcaseApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

Interpretation

The arrows summarize the source-visible entry, role-named types or folders, and framework direction; when nodes come from structural folders, the sequence is a high-level interpretation rather than proof that every adjacent node calls the next. Ownership is claimed only where the next section cites a stored property or assignment. The diagram is intentionally limited to the dominant path into Swift.

Ownership and state

Ownership evidence

ForestUI/ForestView.swift:12 — stored dependency or nearest verified ownership anchor

public struct ForestView: View {
    let forest: Forest
    // ...
}
Owner Object or state Relationship Mutation authority
ForestView Forest (forest) stores or receives Initialized by the owner; the binding is immutable
CustomOneTreeForest String (name) owns value state Initialized by the owner; the binding is immutable
CustomOneTreeForest Int (numTrees) owns value state Initialized by the owner; the binding is immutable
CustomOneTreeForest TreeKind (kind) stores or receives Initialized by the owner; the binding is immutable

Composition arrows indicate a source-visible construction expression or locally owned value state; aggregation means the owner stores or receives a dependency without proving exclusive lifetime ownership.

Class and protocol design

Swift-and-Cplusplus-InteroperabilityShowcase/SwiftCppInteroperabilityShowcaseApp.swift:11 — representative type boundary

struct SwiftCppInteroperabilityShowcaseApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}
Type Responsibility Depends on or conforms to
SwiftCppInteroperabilityShowcaseApp Application entry and top-level composition App
ContentView User-interface presentation and input forwarding View
ForestView User-interface presentation and input forwarding View
CustomOneTreeForest Represents a feature value or composable behavior Concrete collaborators/imported frameworks
MagicForest Defines a closed set of feature states or choices Concrete collaborators/imported frameworks
ForestTests Owns feature behavior and collaborator lifecycle XCTestCase

No local protocol conformance is claimed as protocol-oriented design; external framework conformances are listed only as dependencies.

Access control

Symbol Access Verified effect Likely rationale
MagicForest (ForestBuilder/MagicForest.swift:15) public The symbol is visible to importing modules. Inference: make the declaration available across a module or target boundary.
createForest (ForestBuilder/MagicForest.swift:27) public The symbol is visible to importing modules. Inference: make the declaration available across a module or target boundary.
ForestView (ForestUI/ForestView.swift:14) public The symbol is visible to importing modules. Inference: make the declaration available across a module or target boundary.
ForestView.body (ForestUI/ForestView.swift:18) public Supplies the required View.body witness on the exported ForestView type. Keeps the public ForestView: View conformance usable across the ForestUI module boundary; it is not a separate service API.

Reference code

ForestUI/ForestView.swift:11 — exported view conformance and its required witness

public struct ForestView: View {
    let forest: Forest

    public init(forest: Forest) {
        self.forest = forest
    }

    public var body: some View {
        HStack {
            Text("✨")
            Text(String(forest.forestName))
            Text("✨")
        }.padding(2.0)
        // The remaining source lists each tree in the forest.
    }
}

Swift declarations without a modifier are internal; explicit private, fileprivate, private(set), public, or open entries above are interpreted by language semantics. Objective-C/C samples instead rely on header and implementation boundaries, which are not equivalent to Swift lexical privacy.

Logic ownership and placement

Logic Owning type or file Placement rationale
Application entry and top-level composition SwiftCppInteroperabilityShowcaseApp The source’s App suffix makes this role explicit.
User-interface presentation and input forwarding ContentView, ForestView The source’s View suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
No named application pattern Swift-and-Cplusplus-InteroperabilityShowcase/SwiftCppInteroperabilityShowcaseApp.swift:10 The verified source directly composes concrete framework types; this document avoids forcing a pattern name.

Naming conventions

  • Types: App: SwiftCppInteroperabilityShowcaseApp; View: ContentView, ForestView.
  • Protocols: no local protocol declaration in the scanned source.
  • Methods: createForest.
  • Files: Swift-and-Cplusplus-InteroperabilityShowcase/SwiftCppInteroperabilityShowcaseApp.swift, Swift-and-Cplusplus-InteroperabilityShowcase/ContentView.swift, ForestUI/ForestView.swift, ForestBuilder/MagicForest.swift.

Architecture takeaways

  • SwiftCppInteroperabilityShowcaseApp is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches Forest, ForestBuilder, SwiftUI, ForestTestSupport through a deliberately small high-level chain; the detailed API graph remains inside the cited implementation files.
  • Stored-property evidence identifies lifecycle collaboration; it does not by itself prove exclusive object ownership.
  • Access-control conclusions separate verified language visibility from the likely design rationale.
  • The source does not justify labeling the design protocol-oriented.

Source map

Source file Relevant symbols
Swift-and-Cplusplus-InteroperabilityShowcase/SwiftCppInteroperabilityShowcaseApp.swift SwiftCppInteroperabilityShowcaseApp
Swift-and-Cplusplus-InteroperabilityShowcase/ContentView.swift ContentView
ForestUI/ForestView.swift ForestView
ForestBuilder/ForestBuilder.h Feature implementation
ForestBuilder/MagicForestBuilder.mm Feature implementation
ForestBuilder/MagicForest.swift CustomOneTreeForest, MagicForest
Forest/Forest.cpp Feature implementation
Forest/Forest.h Feature implementation
Forest/Tree.cpp Feature implementation
ForestTestSupport/Tree+Equatable.swift Feature implementation