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 SwiftCppInteroperabilityShowcaseApp → ContentView → SwiftUI 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. |
| Execution model | No structured execution marker indexed; callback threading requires source review. |
| State/event model | No structured observation or publisher-scheduling marker indexed. |
| Key frameworks/packages | Forest, ForestBuilder, Foundation, SwiftUI, ForestTestSupport; these are source dependencies, not architecture labels. |
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
flowchart LR
N1["SwiftCppInteroperabilityShowcaseApp"]
N2["ContentView"]
N3["SwiftUI APIs"]
N1 --> N2
N2 --> N3
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
classDiagram
ForestView o-- Forest : forest
CustomOneTreeForest *-- String : name
CustomOneTreeForest *-- Int : numTrees
CustomOneTreeForest o-- TreeKind : kind
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.
Concurrency, scheduling, and thread safety
Evidence limit: actor isolation, async/await, or Task creation does not by itself prove background-thread execution; Sendable conformance alone does not prove thread-safe mutation.
No source-visible execution, scheduling, or synchronization boundary was found in the indexed source.
@MainActor/MainActor.run, DispatchQueue.main, and RunLoop.main are reported as distinct isolation, queue, and event-loop mechanisms. A plain Task is kept separate from Task.detached; neither is labeled as a background thread.
State propagation, frameworks, and dependencies
Evidence limit: an import proves a source-level compilation dependency at the cited line; it does not prove runtime use, architectural adoption, or whether a Swift package is a direct application dependency.
| Category | Mechanism or module | Verified role | Evidence |
|---|---|---|---|
| Source import | Forest |
The cited file imports this module; runtime use and architectural role are not inferred. | Forest/Forest.h:17 |
| Source import | ForestBuilder |
The cited file imports this module; runtime use and architectural role are not inferred. | ForestBuilder/ForestBuilder.h:17 |
| Source import | Foundation |
The cited file imports this module; runtime use and architectural role are not inferred. | Forest/Forest.h:8 |
| Source import | SwiftUI |
The cited file imports this module; runtime use and architectural role are not inferred. | ForestUI/ForestView.swift:9 |
| Source import | ForestTestSupport |
The cited file imports this module; runtime use and architectural role are not inferred. | ForestTestSupportTests/ForestTestSupportTests.swift:10 |
| Source import | ForestUI |
The cited file imports this module; runtime use and architectural role are not inferred. | Swift-and-Cplusplus-InteroperabilityShowcase/ContentView.swift:9 |
receive(on:) describes downstream delivery scheduling, while subscribe(on:) describes upstream subscription/request/cancel scheduling. An import Combine alone establishes neither behavior nor a Store, reducer, Redux, or other application architecture.
Class and protocol design
Swift-and-Cplusplus-InteroperabilityShowcase/SwiftCppInteroperabilityShowcaseApp.swift:11 — representative type boundary
@main
struct SwiftCppInteroperabilityShowcaseApp: App {
// ...
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. |
body (ForestUI/ForestView.swift:18) |
public |
The symbol is visible to importing modules. | Inference: make the declaration available across a module or target boundary. |
Reference code
ForestBuilder/MagicForest.swift:15 — representative boundary
public init(oakForestName name: String, numTrees: Int) {
self.name = name
self.numTrees = numTrees
kind = TreeKind.Oak
}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
SwiftCppInteroperabilityShowcaseAppis 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 |
Cited implementation, SwiftCppInteroperabilityShowcaseApp |
ForestUI/ForestView.swift |
Cited implementation, SwiftUI, ForestView |
ForestBuilder/MagicForest.swift |
Cited implementation, CustomOneTreeForest, MagicForest |
Forest/Forest.h |
Forest, Foundation, Feature implementation |
ForestBuilder/ForestBuilder.h |
ForestBuilder, Feature implementation |
ForestTestSupportTests/ForestTestSupportTests.swift |
ForestTestSupport, ForestTests |
Swift-and-Cplusplus-InteroperabilityShowcase/ContentView.swift |
ForestUI, ContentView, ContentView_Previews |
ForestBuilder/MagicForestBuilder.mm |
Feature implementation |
Forest/Forest.cpp |
Feature implementation |
Forest/Tree.cpp |
Feature implementation |
ForestTestSupport/Tree+Equatable.swift |
Feature implementation |
ForestUI/Tree+UI.swift |
Feature implementation |
Swift-and-Cplusplus-InteroperabilityShowcase/EnchantedForest.swift |
Feature implementation |