Skip to content
imaged

Swift setup

iOS 18+ · macOS 15+ · Apple silicon

Slab ships for Apple platforms as a Swift package:

ios/
  Package.swift
  Slab.xcframework/        iPhone/iPad (arm64), Simulator (arm64), Mac (arm64)
  Sources/SlabSwift/       the Swift API
  • Slab is the binary framework with the C interface. Objective-C uses it directly (see Objective-C).
  • SlabSwift is the Swift API on top of it: classes, throws, async streaming, and camera frames from CVPixelBuffer.

Examples: Classify, Faces, Chat, CameraDetector.

Add to an app

In Xcode: File › Add Package Dependencies › Add Local…, choose the ios folder, and add the SlabSwift library to your app target. Or in a Package.swift:

.package(path: "path/to/ios")
// target dependency:
.product(name: "SlabSwift", package: "ios")

Minimums: iOS 18, macOS 15, Apple silicon. Simulator builds run on Apple silicon Macs (arm64).

Use

import SlabSwift

let config = try SlabConfig(cacheDirectory: NSTemporaryDirectory(), license: licenseString)
let session = try SlabSession(config: config, path: modelPath, warmup: true)

let output = try session.run(SlabInput(frame: try SlabFrame(contentsOf: imagePath)))
for c in output.topK(5) {
    print(c.label, c.score)
}

Text generation:

let input = SlabInput(prompt: "Describe this image.")
for try await piece in session.generate(input) {
    print(piece, terminator: "")
}

API

The classes are the same five objects as in every Slab API, with a Slab prefix:

Swift Notes
SlabConfig(modelsDirectory:cacheDirectory:backend:threads:license:) License errors throw SlabError with licenseReason
SlabSession(config:path:backend:warmup:contextLength:) Opens the model when path is given
session.run(_:into:) Returns the SlabOutput
session.generate(_:into:) AsyncThrowingStream<String, Error>; stopping the loop or cancelling the task cancels the generation
session.start(_:into:), session.nextToken(_:) Pull text yourself, on your own thread
session.inspect(_:), backendProblem(_:), inputs, outputs, labels, meta(_:) Model description and checks
setResize, setColorBGR, setNormalization Override the model file's preprocessing
SlabFrame(contentsOf:), SlabFrame(data:), SlabFrame(pixels:...) JPEG/PNG file or bytes, or raw pixels (copied)
SlabFrame(pixelBuffer:) Camera frames: 32BGRA or 32RGBA CVPixelBuffer
frame.crop(_ rect: CGRect) A view sharing the parent's pixels
SlabInput(frame:prompt:), setTensor(_:index:name:) [Float], [Int64] or [UInt8] tensors (copied)
input.addMessage(role:text:image:), maxTokens, temperature, setJSONSchema ... Prompts and sampling
output.topK(_:), output.boxes(...) SlabClassification, SlabBox (rect, landmarks in image pixels)
output.floats(_:), int64s, bytes, shape, toFrame Tensor copies
output.text, fullText, finishReason, token counts Generation results

All failures throw SlabError (code, message). Objects free themselves when released. One operation at a time per session; separate sessions run in parallel.

Licensing

The runtime license is bound to your app's bundle ID and App ID prefix (iOS) or bundle ID and Keychain group (macOS). The Simulator checks the bundle ID only.