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")

Use Xcode 16 or later (Swift tools 6.0). 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

List the app's bundle identifier in its runtime license. Each extension that loads Slab (widget, share or notification extension) has its own ID and must also be listed. The same app-ID license works on supported Apple devices, the arm64 simulator, Android and macOS. No Apple App ID prefix or Keychain group is required for new licenses.

See Licenses & renewals for primary IDs, variants and upgrades from older SDKs.

License details

Slab 0.2.0 exposes the attached license on Config and Session. Use it to display expiry and send renewal reminders to whoever maintains the license.

Config member Value
licenseSecondsLeft Seconds remaining
licenseExpiresAt Expiry, Unix seconds
licenseNotBefore Start of validity, Unix seconds
licenseIssuedAt Issue time, Unix seconds
licenseKind License kind
licenseFeatures Enabled feature flags
licenseID License ID

Seconds remaining becomes negative after expiry and is 0 without a license. Kind values are 0 none, 1 production, 2 trial and 3 pilot. Feature bits are 1 core, 2 graph models, 4 generative, 8 GPU, 16 NPU and 32 protected models.

The session object exposes the same license members.

For protected models, generate a license with your model key in the console. Attach that license before opening the pack. See licensing for model protection and renewals.