Sandboxed – On-Device AI for iOS Developers

Episode 11

When to Use ML (And When Not To)

Don't use AI to sort a list. We explain the 'Hardware Tax' of inference and why deterministic code (Regex) often beats probabilistic models (BERT) for user experience.

The mark of a Senior Engineer is knowing when to reject AI. We explore the "Scalpel vs. Sledgehammer" decision matrix, the Hardware Tax of inference, and why Regex beats a neural network for parsing structured data.

🧠The "AI Everywhere" Trap

Every engineering team goes through a predictable lifecycle with AI. First, the Novelty Phase: AI is magic, a universal solvent for any problem. Then the Utility Phase: the app balloons by 200MB, the battery drains 15% faster, and the model starts making weird mistakes.

The goal is to reach the Architectural Phase—knowing that AI is not a cleaner version of code, but a fundamentally different kind of material.

Standard code is a Logician: deterministic, absolute, never wrong unless you gave it the wrong rules. ML is a Statistician: it observes data, calculates correlations, and explicitly reserves the right to be wrong 2% of the time.

⚙️Deterministic vs. Probabilistic

Code 1.0 operates on a rigid contract. If Array.sort() or JSONDecoder fail 1% of the time, our entire software stack collapses.

Code 2.0 operates on a statistical contract. A wrong prediction is not a "bug"—it is a valid sample from the tail end of a probability distribution.

The UX Litmus Test: Funny vs. Fatal

"Funny" Failure: AI groups a cat into the "Dogs" album. The user chuckles and moves it.

"Fatal" Failure: AI reads a $100 check as $1,000. Trust evaporates immediately.

🛠️The Classic Alternatives

Regex: The Undefeated Champion

For extracting phone numbers, emails, dates, or structured codes, Regex is superior in every metric: microseconds vs. milliseconds, 100% vs. 99% accuracy, free vs. expensive compute.

import Foundation

let text = "Contact me at bob@example.com"
let detector = try! NSDataDetector(
    types: NSTextCheckingResult.CheckingType.link.rawValue
)
let matches = detector.matches(
    in: text,
    range: NSRange(text.startIndex..., in: text)
)
// Deterministic. Extremely fast. Standardized.

Classic Computer Vision

Apple's Vision framework handles both AI tasks (probabilistic) and classic tasks (deterministic). VNDetectBarcodesRequest scans for black-and-white patterns deterministically—it doesn't "hallucinate" a URL.

For keyword search: Before reaching for a vector database, use SQLite Full Text Search (FTS). It's deterministic, fast, and "good enough" for 99% of use cases.

🔍The Systems Budget

When choosing between a native Swift API and a Core ML model, you're choosing a billing structure:

  • 1.Startup Cost: Logic = zero. AI = the "Cold Start" penalty (loads, compiles, allocates).
  • 2.Memory: Logic = kilobytes. AI = megabytes/gigabytes of weights and scratchpad.
  • 3.Thermals: Logic = cool (integer math). AI = hot (the NPU "Spin-Up Tax").
  • 4.Complexity: Logic = O(1) to O(N), predictable. AI = variable, depends on thermals and partitioning.

The Verdict: Only pay the "AI Tax" when the feature value exceeds these costs. For "Smart Crop," the 50MB cost is worth it. For "Email Validation," it's bankruptcy.

The Decision Matrix

  • Is the data Unstructured? (Images, audio, natural language) — Investigate ML.
  • Is the data Structured? (JSON, dates, rigid codes) — Use Logic, Regex, or SQLite.
  • Is the problem "Fuzzy"? ("Is this similar?" "What is this?") — Use ML. These are perception problems.
  • Is the problem "Logical"? ("Sort this list." "Calculate the tax.") — Use deterministic code.
  • Can you tolerate Error? (Recommendations, filters) — ML is helpful at 90%.
  • Is Error fatal? (Medical dosage, financial logic) — Use strictly gated logic.

🎯Key Takeaways

  • 1.Logic for structure, ML for perception—Use the Scalpel (regex, formatters, detectors) for structured data and the Industrial Slicer (Core ML, Vision) for unstructured problems.
  • 2.The Hardware Tax is real—Model load time, memory, and battery cost must be measured. If the cost exceeds the benefit, the Scalpel wins.
  • 3.Design for fallibility—If you use a probabilistic model, budget for safety guards: Edit buttons, Confirm dialogs, and Undo actions. That UI is part of the AI Tax.
  • 4.Exhaust the classics firstNSDataDetector, SQLite FTS, and deterministic Vision requests solve 80% of data processing tasks without any model overhead.

About Sandboxed

Sandboxed is a podcast for iOS developers who want to add AI and machine learning features to their apps—without needing a PhD in ML.

Each episode, we take one practical ML topic—like Vision, Core ML, or Apple Intelligence—and walk through how it actually works on iOS, what you can build with it, and how to ship it this week.

If you want to build smarter iOS apps with on-device AI, subscribe to stay ahead of the curve.

Ready to dive deeper?

Next, we switch on the Industrial Slicer. We build our first real AI feature—a real-time image classifier using the Vision Framework and Core ML.

Stay in the Loop

Get on-device AI insights, new episode alerts, and exclusive iOS development content delivered to your inbox.

No spam. Unsubscribe anytime.

When to Use ML (And When Not To) | Sandboxed Podcast