# Add the model to Xcode and run Core AI NDArray

- **Lesson ID:** 04
- **Goal:** Add `clothing.aimodel`, load `AIModel`, prepare function `main`, and run one correctly shaped image tensor.
- **Human lesson:** [04-xcode-and-core-ai.html](04-xcode-and-core-ai.html)

## Prerequisites

- `clothing.aimodel` from lesson 03.
- An Xcode 27 project, the Core AI SDK, and the Metal Toolchain installed.

## Inputs, outputs, and artifacts

- **Inputs:** Model file; one 28×28 grayscale image represented as float32 values 0–1.
- **Outputs:** Core AI `logits` output and a checked model interface.
- **Artifacts:** Xcode target containing `clothing.aimodel`; model viewer notes; a working inference adapter.

## Agent build steps

1. Drag `clothing.aimodel` into the Xcode target and confirm it is in the resources/model build phase.
2. Inspect the model viewer for function `main`, input `image`, output `logits`, shapes, and element types.
3. Link/import Core AI and locate the bundled URL for resource `clothing` with extension `aimodel`.
4. Load `AIModel(contentsOf:)` (or the exact SDK equivalent), then load function `main`. Prefer explicit `AIModel.specialize` during setup when it is supported.
5. Convert a 28×28 grayscale buffer into the SDK's `NDArray` with shape `[1,1,28,28]` and `.float32`.
6. Run `function.run(inputs: ["image": image], states: [:], outputViews: nil)` and read `outputs["logits"]`.

## Constraints

Core AI, not Core ML. Use the exact SDK signatures in the installed Xcode 27 docs. Helper names in the lesson are adapters. Tensor contract is NCHW `[1,1,28,28]` float32 0–1, named `image`, returning `[1,10]` logits named `logits`. Prepare once. Never prepare again on each photo pick.

## Key concepts

- Xcode 27 setup: add `clothing.aimodel`, link Core AI, install the Metal Toolchain.
- Load `AIModel`, then `loadFunction(named: "main")`.
- Build an `NDArray` as 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`, and `logits` in 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.

## Acceptance checks

- Xcode builds with the asset and Metal Toolchain.
- Model viewer names and shapes match the conversion output.
- A zero/sample tensor runs without shape or type errors and returns ten logits.
- Continue to [the SwiftUI brief](05-swiftui-clothing-app.llms.md).

## Environment setup

Use Xcode 27 on macOS. Create an iOS or macOS SwiftUI App target, add the exported `.aimodel` to the target's model/resources build phase, add the **Core AI** framework under Frameworks, Libraries, and Embedded Content, and install the **Metal Toolchain** in Xcode Settings > Components. Confirm the installed toolchain with:

```bash
xcode-select --install
xcodebuild -version
```

Build the bundled model once before you add UI. Core AI and `NDArray` names can change between SDK versions, so check the exact names in the Xcode docs you have installed.
