SDKs
iOS / macOS
Native Swift SDK for on-device ML inference
The Swift SDK provides native bindings to the Xybrid runtime via UniFFI for iOS and macOS applications.
The Swift SDK is in early access. The API is stable but the package is not yet published as a release. Install via Swift Package Manager from source, or use the Flutter SDK for cross-platform development.
Installation
Add the Xybrid package via Swift Package Manager:
dependencies: [
.package(url: "https://github.com/xybrid-ai/xybrid", from: "0.1.0")
]Or in Xcode: File > Add Package Dependencies and enter the repository URL.
Supported platforms: iOS 13.0+, macOS 10.15+
Quick Start
import Xybrid
// Load a model from the registry
let loader = ModelLoader.fromRegistry(modelId: "kokoro-82m")
let model = try loader.load()
// Run inference
let envelope = Envelope.text("Hello, world!")
let result = try model.run(envelope: envelope)
if result.success {
print("Output: \(result.text ?? "")")
print("Latency: \(result.latency)s")
}Model Loading
From Registry
let loader = ModelLoader.fromRegistry(modelId: "whisper-tiny")
let model = try loader.load()From Local Bundle
let loader = ModelLoader.fromBundle(path: bundlePath)
let model = try loader.load()Input Envelopes
Audio (Speech Recognition)
let envelope = Envelope.audio(pcmData: audioData, sampleRate: 16000, channels: 1)
let result = try model.run(envelope: envelope)
print("Transcription: \(result.text ?? "")")Text (Text-to-Speech)
// Simple text
let envelope = Envelope.text("Hello, how are you?")
// With voice and speed
let envelope = Envelope.text("Hello", voice: "af_heart", speed: 1.0)
let result = try model.run(envelope: envelope)
if let audioBytes = result.audioBytes {
// Play or save audio
}Embedding
let envelope = Envelope.embedding(data: [0.1, 0.2, 0.3])
let result = try model.run(envelope: envelope)
if let vector = result.embedding {
// Process embedding vector
}Result Handling
let result = try model.run(envelope: envelope)
if result.success {
switch result.outputType {
case "text":
print("Text: \(result.text!)")
case "audio":
playAudio(result.audioBytes!)
case "embedding":
process(result.embedding!)
default:
break
}
print("Latency: \(result.latency)s") // TimeInterval in seconds
} else {
print("Error: \(result.error ?? "Unknown")")
}XybridResult Properties
| Property | Type | Description |
|---|---|---|
success | Bool | Whether inference succeeded |
error | String? | Error message if failed |
outputType | String | "text", "audio", or "embedding" |
text | String? | Text output (ASR, LLM) |
audioBytes | Data? | Audio output (TTS) |
embedding | [Float]? | Embedding vector |
latencyMs | UInt32 | Inference latency in ms |
isFailure | Bool | Convenience: !success |
latency | TimeInterval | Latency in seconds |
Error Handling
The SDK uses a Swift error enum for type-safe error handling:
do {
let model = try ModelLoader.fromRegistry(modelId: "kokoro-82m").load()
let result = try model.run(envelope: envelope)
} catch XybridError.ModelNotFound(let modelId) {
print("Model not found: \(modelId)")
} catch XybridError.InferenceFailed(let message) {
print("Inference failed: \(message)")
} catch XybridError.InvalidInput(let message) {
print("Invalid input: \(message)")
} catch XybridError.IoError(let message) {
print("I/O error: \(message)")
} catch {
print("Unexpected error: \(error.localizedDescription)")
}Type Aliases
The SDK provides short aliases for convenience:
| Alias | Full Type |
|---|---|
ModelLoader | XybridModelLoader |
Model | XybridModel |
Envelope | XybridEnvelope |
Result | XybridResult |
Platform Support
| Platform | Status | Accelerators |
|---|---|---|
| iOS | Supported | Metal, CoreML, ANE |
| macOS (Apple Silicon) | Supported | Metal, CoreML, ANE |
| macOS (Intel) | Supported | CPU |