skip to content

Should an iOS team share Swift code between its app and a Linux server, and how would you keep the shared module portable?

level: principalimportance: should knowfreq 20%

answer

  1. what is pure enough to share
  2. old app versions stay installed
  3. two compilers, one source
  4. a Linux CI lane is the price

basics

~20 s

Share only small, pure Swift such as wire models and validation rules, never UI or Apple-framework code; keep it portable with Linux CI, a pinned compiler on both sides and FoundationEssentials-level dependencies, and version it as a contract.

solid answer

~50 s

It pays when both sides are already Swift and the shared surface is small and pure: request and response models, validation rules, domain calculations. It does not pay when the shared code would pull in UIKit, SwiftUI or other Apple frameworks, Objective-C patterns, or server framework types. Sharing removes duplicate definitions, but not wire compatibility: app versions stay installed for months, so the server must keep accepting what old builds send, and the shared types are a versioned contract. Keep the module portable by depending on Foundation's core types only (FoundationEssentials where you can), avoiding `@objc` and Apple imports, building it in a Linux CI job, and pinning the same Swift release on both sides: Xcode's bundled 6.4 and a `.swift-version` of 6.4 for swiftly. The alternatives, a shared schema with generated code or plain contract tests, win when either side might not stay Swift.

go deeper

for a junior

Recall that Swift runs on Linux, so pure code without Apple frameworks, such as models and validation, can compile for both an app and a server.

for a middle

Explain what keeps shared code portable: no @objc or Apple imports, Foundation's core types only, and a Linux build in CI.

for a senior

Show the operational side: pinning one compiler release on Xcode and Linux, one language mode, and diagnosing a shared module that fails only on Linux.

for a principal

Own the tradeoff: shared code buys consistency but couples release trains and hides the wire contract, so scope it narrowly, version it, and prefer a schema when either side may leave Swift.

## The decision Swift runs on Linux with the same compiler, the same standard library and, since Swift 6, the same implementation of Foundation's core types. A team with an iOS app and a Swift server can therefore compile one module into both. Whether it *should* is a judgment about coupling, not a technical question: the code will build; the costs come later. **Share when** most of these hold: - both sides are Swift today and expected to stay Swift; - the shared surface is small and **pure**: wire models, validation rules, pricing or scheduling calculations, error codes; - the team will own a Linux build of that module, not just the Mac one; - someone owns the rules for changing it. **Don't share when** the module would need UIKit, SwiftUI, SwiftData or another Apple framework, relies on Objective-C features, would import server-framework types such as Vapor's, or when the "shared" code is really two different models that happen to look alike today. ## What sharing does and does not buy | Benefit | Cost or limit | |---|---| | One definition of each request and response type | Deployed apps keep old definitions; wire compatibility is still required | | Validation behaves identically on both sides | A rule change ships to the server instantly, to apps only on update | | Compile-time breakage instead of runtime drift | Both builds break together; release trains become coupled | | One language for the team | A Linux CI lane, a second toolchain to upgrade | The trap is believing sharing removes the contract. An app installed months ago still sends the old shape. Adding a field means making it optional or defaulted for old clients; removing one waits until the server no longer sees old builds. Shared code hides that from the compiler, so it must be enforced by review and tests. ## Keeping the module portable 1. **Dependencies:** standard library plus Foundation's core types. Where possible, `#if canImport(FoundationEssentials) import FoundationEssentials #else import Foundation #endif`, which keeps internationalization data out of server binaries and makes accidental reliance on formatting APIs visible. 2. **No Apple-only constructs:** no `@objc`, `#selector`, KVO or Apple framework imports; Linux has no Objective-C runtime and the compiler rejects them there. 3. **One compiler release on both sides:** the app builds with the toolchain Xcode bundles (Xcode 27 ships Swift 6.4); pin Linux to the same release with a committed `.swift-version` for swiftly. Different compilers can accept different code, and the Mac build is not evidence the Linux build passes. 4. **An explicit language mode:** declare the shared target's Swift 5 or Swift 6 mode and migrate it deliberately; once it is in Swift 6 mode, a data-race error blocks the app and the server build at once. 5. **A Linux job in CI** that builds and tests the module on every change; nothing else catches platform breaks. 6. **Versioning:** tag the module and let the server and app adopt new versions deliberately. ## Alternatives a lead should weigh - **A shared schema** (for example an OpenAPI document) with code generated for each side: language-neutral, makes the contract explicit, survives a server rewrite in another language. - **Contract tests** against recorded payloads: no shared code, but drift is caught automatically. - **Duplicate small models** consciously: cheap when the surface is a handful of types. ## Signals that sharing is going wrong - The shared module grows `#if os(iOS)` branches or `canImport` guards around Apple frameworks: it is no longer pure. - Server releases wait for an app release, or the other way round, because a shared type changed. - The Linux job is "temporarily" disabled, so Mac-only code slips in unnoticed. - Types gain fields that only one side uses, turning the module into a dumping ground rather than a contract. Any of these is a cue to split the module or fall back to a schema. ## A defensible default Start with the narrowest useful module, models and validation only, with its own Linux CI job and a written rule that every change stays readable by the oldest supported app version. Expand only when the team has lived with that discipline for a few releases. If the server might move off Swift, prefer the schema approach from the start.

  • The shared module builds in Xcode but fails on the Linux CI job. What do you check?
    First the compiler: compare `swift --version` on both, since Xcode's bundled release and the Linux toolchain may differ; pin Linux with `.swift-version`. Then the platform: an `@objc` or `#selector`, an Apple-only import, a `Darwin` import, or `URLSession` without FoundationNetworking. A Mac build cannot reveal any of these, which is why the Linux job exists.
  • How do you add a field to a shared response type without breaking installed apps?
    Make it optional or give it a default, ship the server change first, and let apps adopt it on update. Old apps decode the response without the field and keep working. Removing or renaming a field waits until analytics show no supported app version still reads it, which shared code alone cannot tell you.
  • When would you choose a shared schema with generated code over a shared Swift module?
    When either side might not stay Swift, when other clients such as a web front end consume the same API, or when you want the contract reviewed as a document rather than inferred from code. Generation keeps each side's types native while making breaking changes visible in the schema diff.

  • Says sharing the models removes the need for backward-compatible API changes.
  • Shares view models or SwiftUI types with the server.
  • Relies on the Mac build to prove the Linux build works.
  • Lets the server and app build the shared module with different Swift releases unknowingly.
  • Treats a shared module as free because both sides already use Swift.

Assumes Swift 6.4 on both sides (Xcode 27 bundles it); Swift 6.0 brought the shared Swift implementation of Foundation and the FoundationEssentials module to Linux.