पाठ 24 / 25

Beyond Apple Apps: Server Swift, Interop and Cross-Platform

Know where else Swift is used and how it interoperates with other languages.

Swift outside the App Store

Swift is not limited to iPhone apps. Server-side Swift frameworks, most prominently Vapor (and Hummingbird), build web APIs on top of SwiftNIO, Apple's non-blocking networking library, with async/await throughout; Apple itself has described using Swift on the server for some services. Swift runs on Linux (with official Docker images) and Windows, and the Swift SDK for static Linux produces dependency-free binaries for deployment. There is ongoing work on Embedded Swift for microcontrollers and on WebAssembly. Interoperability is a strength: Swift calls C libraries directly via module maps, has bidirectional Objective-C interop (essential for older Apple APIs and codebases), and since Swift 5.9 supports C++ interoperability, letting projects adopt Swift incrementally in C++ codebases. For cross-platform mobile work, some teams share Swift business logic across platforms, though Kotlin Multiplatform and Flutter are more common choices for that goal. Whatever the platform, the language knowledge from this course transfers directly.

A minimal Vapor route

The same Swift language features power server code.

import Vapor

struct OrderDTO: Content {           // Content = Codable + HTTP encoding
    let id: String
    let status: String
    let totalPaise: Int
}

func routes(_ app: Application) throws {
    app.get("health") { _ in "ok" }

    app.get("orders", ":id") { req async throws -> OrderDTO in
        guard let id = req.parameters.get("id") else { throw Abort(.badRequest) }
        guard let order = try await OrderRepository.find(id, on: req.db) else { throw Abort(.notFound) }
        return OrderDTO(id: order.id, status: order.status.rawValue, totalPaise: order.totalPaise)
    }
}

Use Swift where its strengths matter

Swift shines where safety, performance and Apple-platform integration matter. For a team already using another backend language, sharing models via OpenAPI may be simpler than adopting server-side Swift.

त्वरित जाँच: Which framework is the best-known option for building web APIs in Swift?

  • Vapor
  • Django
  • Express
  • Spring
Answer

Vapor — Vapor is the most widely used server-side Swift framework, built on SwiftNIO.