Environment setup
Use Xcode 27 (the SDK version this course uses) on macOS. Create an iOS or macOS SwiftUI app target, set the target's deployment settings, and drag the exported .aimodel into the target's model/resources build phase. In Xcode, add the Core AI framework under Frameworks, Libraries, and Embedded Content. Install the Metal Toolchain from Xcode Settings > Components.
xcode-select --install
xcodebuild -version
# In Xcode: File > New > Project > App, Interface: SwiftUI
# Add CoreAI to the target and add the .aimodel to Copy Bundle Resources.
# Xcode Settings > Components > install Metal Toolchain.Build once with the bundled model before you add image or chat UI. Core AI and NDArray names can change between SDK versions. Check the exact names in the Xcode docs you have installed. Do not copy a name you have not checked.
Add and inspect the asset
- Open an Xcode 27 iOS or macOS app target.
- Drag
clothing.aimodelinto the project. - Make sure it is in the app's resources or model build phase shown by Xcode.
- Open the Core AI model viewer. Check the function name, tensor names, shapes, and data types before you write Swift.
The portable file is .aimodel. A compiled, architecture-specific file uses .aimodelc.
Load and run
import CoreAI
let modelURL = Bundle.main.url(
forResource: "clothing",
withExtension: "aimodel"
)!
let model = try await AIModel(contentsOf: modelURL)
let function = try model.loadFunction(named: "main")
// Build this from 28x28 grayscale pixels in [0, 1].
// NDArray is float32, NCHW, with shape [1, 1, 28, 28].
let image: NDArray = makeImageNDArray(
values: pixels,
shape: [1, 1, 28, 28],
dataType: .float32
)
let outputs = try function.run(
inputs: ["image": image],
states: [:],
outputViews: nil
)
let logits = outputs["logits"]
main, image, and logits are the names from the conversion lesson. Confirm them in the model viewer. Build the NDArray from the real NDArrayDescriptor and initializer in the SDK you have installed.
Preprocess like training
For a picked photo, resize the CGImage to 28×28, draw it in grayscale, and scale its pixel values to 0–1. Fashion-MNIST pictures have a dark background and light clothing, so a normal photo may need a crop, contrast change, or inversion to look like the training examples. The next lesson shows the image path.
Load the model when the app starts
Load and specialise the model during app setup, not inside the photo picker callback. Specialise means get the model ready for this device. You can use the path below and keep the prepared model in memory so later photos are faster:
let model = try await AIModel.specialize(contentsOf: modelURL)
let function = try model.loadFunction(named: "main")
The exact method names can change. Check them against the Core AI documentation that ships with the Xcode 27 SDK.
Sources: Core AI documentation and WWDC26 sessions 324 and 325.
Key concepts
- Xcode 27 setup: add
clothing.aimodel, link Core AI, install the Metal Toolchain. - Load
AIModel, thenloadFunction(named: "main"). - Build an
NDArrayas float32 NCHW[1, 1, 28, 28]from 28x28 grayscale pixels scaled to 0-1. - Prepare once at app startup. Do not prepare inside the photo picker callback.
Takeaways
- Confirm
main,image, andlogitsin the model viewer before you write Swift. - Build once with the bundled model before you add picker UI.
- Check exact API names in the Core AI docs that ship with your Xcode 27 SDK.