Swift and Kotlin Prompts That Write Native Code, Not Documentation: A Field Guide for iOS and Android Teams

You've seen it: you ask an LLM for a SwiftUI screen, and it replies with a textbook definition of @State and a code snippet that uses NavigationView — deprecated since iOS 16. Or you ask for a Kotlin coroutine flow, and it gives you GlobalScope.launch with a side of memory leak. The model knows the syntax but not the platform's pulse. That's the gap this guide closes.

Below are prompts that force the LLM to write native code that compiles, respects lifecycle, and doesn't hallucinate APIs. Each prompt is a tool for a specific job: generating SwiftUI views and ViewModels, handling Kotlin coroutines and lifecycle, diagnosing crashes from stack traces, migrating from UIKit to SwiftUI or Java to Kotlin, and managing memory with ARC and smart casts. We'll also flag where the LLM reliably accelerates work and where it produces outdated APIs that only the compiler and device tests can catch.

If you need a SwiftUI screen that doesn't break on the next iOS beta

The model's default move is to regurgitate whatever SwiftUI looked like in its training data. That means NavigationView, @ObservedObject everywhere, and no @MainActor. The fix is to constrain the model to the target OS version and the current API surface.

Prompt: SwiftUI screen generator with version guard

You are a senior iOS engineer. Generate a SwiftUI screen for iOS 17+ that displays a list of articles with pull-to-refresh and navigation to a detail view.
Constraints:
- Use only APIs available in iOS 17.0 and later. Explicitly avoid NavigationView, use NavigationStack.
- Use @Observable macro for the view model (Observation framework), not ObservableObject.
- Mark the view model @MainActor.
- Provide a preview with mock data.
- Output only the Swift code, no explanations.

Usage example: Run this for a news feed screen. The model returns a NavigationStack with .refreshable, an @Observable class, and a preview. Without the version guard, you'd get NavigationView and ObservableObject — both still compile but are on the deprecation path.

Always name the iOS version LLMs default to iOS 15-era SwiftUI. Stating “iOS 17+” and listing forbidden APIs cuts the cleanup time by half.

If your ViewModel is a tangle of published properties and async calls

SwiftUI ViewModels often become dumping grounds for business logic, leading to race conditions and retain cycles. The LLM can refactor them if you give it a clear contract. More on the topic: “Go, LLM, and the Race Against Races: How to Prompt for Idiomatic Concurrency, Explicit Errors, and Context-Aware Code”.

Prompt: ViewModel refactor with async/await and cancellation

Refactor the following SwiftUI ViewModel to use async/await and structured concurrency.
Requirements:
- Replace completion handlers with async functions.
- Use Task and cancellation checks to avoid updating UI after the view disappears.
- Ensure all UI updates happen on the MainActor.
- Add a loading state and error handling.
- Keep the public interface unchanged.
Input: [paste your ViewModel code]

Usage example: Paste a ViewModel that uses DispatchQueue.main.async and URLSession.dataTask. The model returns a version with async methods, Task stored as a property, and defer to reset loading. You'll still need to test cancellation on a real device — the model can't simulate view lifecycle.

If your Kotlin coroutines leak or freeze the UI

Kotlin's coroutines are powerful but easy to misuse. The LLM often suggests GlobalScope or forgets to tie coroutines to lifecycle. A prompt that enforces structured concurrency and lifecycle awareness fixes that. More on the topic: “Kotlin Prompts That Actually Ship: 12 Android Dev Recipes from Compose to Coroutines and Room”.

Prompt: Coroutine-safe repository with lifecycle awareness

You are a Kotlin Android expert. Write a repository class that fetches user data from a remote API and caches it in memory.
Constraints:
- Use kotlinx.coroutines with structured concurrency.
- Expose a Flow that can be collected in a ViewModel with lifecycle awareness.
- Handle exceptions and retry once on network failure.
- Do not use GlobalScope.
- Provide a ViewModel that collects the Flow using repeatOnLifecycle.
- Output only the Kotlin code.

Usage example: The model returns a repository with flow { } and catch, and a ViewModel using viewModelScope.launch with repeatOnLifecycle. Without the explicit “no GlobalScope” rule, you'd get a leaky coroutine.

The compiler is your co-pilot LLMs write plausible coroutine code that still violates structured concurrency. Always compile and run a leak check with LeakCanary.

If you're staring at an Xcode or Logcat stack trace

Stack traces are where LLMs shine — if you feed them the raw trace and ask for a diagnosis, not a guess. The prompt must force the model to trace the call path and identify the likely cause.

Prompt: Crash diagnosis from stack trace

You are a mobile debugging expert. Analyze the following crash stack trace from Xcode.
Identify the root cause, the line of code responsible, and suggest a fix.
If the trace is from Swift, consider ARC and optionals. If from Kotlin, consider NullPointerException and coroutine cancellation.
Trace: [paste stack trace]
Output: a numbered list of steps to reproduce, the cause, and the fix.

Usage example: A crash log with EXC_BAD_ACCESS in objc_release often means an over-released object. The model will point to a missing weak reference. For Android, a NullPointerException in a coroutine might be due to a race condition — the model can suggest CoroutineExceptionHandler. More on the topic: “Kotlin and Android Development: The Myths That Cost Beginners a Year — and How the Stack Actually Works”.

If you're migrating from UIKit to SwiftUI or Java to Kotlin

Migrations are tedious but pattern-heavy. The LLM can translate screens and classes if you give it the source and the target paradigm.

Prompt: UIKit to SwiftUI migration

Convert the following UIKit view controller to SwiftUI.
Preserve all functionality, including navigation, table view, and networking.
Use iOS 17 APIs.
Output only the SwiftUI code, with comments explaining the mapping.
Input: [paste view controller code]

Usage example: A UITableViewController with a custom cell becomes a List with ForEach. The model handles the mapping, but you must manually check that data binding works — the model might miss @State for local UI state.

Prompt: Java to Kotlin migration

Convert the following Java class to idiomatic Kotlin.
Use data classes, null safety, and extension functions where appropriate.
Avoid platform types.
Output only the Kotlin code.
Input: [paste Java class]

Usage example: A Java POJO with getters and setters becomes a data class. The model might leave !! operators — you'll need to replace them with safe calls.

If you're fighting memory issues: ARC cycles and Kotlin smart casts

Memory bugs are subtle. The LLM can help spot retain cycles in Swift closures or smart cast failures in Kotlin, but only if you ask for a static analysis. More on the topic: “Swift and iOS Development (SwiftUI): Why "Learning the Syntax in a Couple of Days" Is a Myth and How to Actually Master the Language”.

Prompt: ARC retain cycle detection

Analyze the following Swift code for potential retain cycles.
Identify any closures that capture self strongly and suggest weak/unowned capture lists.
Explain why each cycle occurs.
Input: [paste Swift code]

Usage example: A closure stored in a property that references self without [weak self] is a classic cycle. The model flags it and shows the fix.

Prompt: Kotlin smart cast failure diagnosis

The following Kotlin code fails to compile due to a smart cast error.
Explain why the smart cast is impossible and suggest a fix using local variables or safe casts.
Input: [paste code]

Usage example: A mutable property checked for null then used without a local copy triggers “smart cast to 'String' is impossible”. The model suggests val local = nullable?: return.

Where LLMs genuinely speed you up — and where they don't

After running these prompts on real projects, a pattern emerges:

Task LLM helpful? Why
Generating boilerplate SwiftUI views Yes The model knows common patterns
Refactoring to async/await Yes, with constraints It can convert completion handlers reliably
Diagnosing crashes from stack traces Sometimes Depends on trace clarity; always verify
Migrating UIKit to SwiftUI Partially Complex screens need manual review
Memory management (ARC, smart casts) Yes for detection Fixes need human judgment
Writing coroutine code With strict rules Default output leaks
The compiler is the ultimate judge No matter how good the prompt, always compile and run on a device. LLMs hallucinate API availability and lifecycle behavior.

Putting it to work

Start with one screen or one crash. Copy the prompt, fill in your code, and run it. Then compile. Iterate. The LLM won't replace your judgment, but with these constraints it becomes a fast junior developer who never gets tired — as long as you review the pull request.

Rules and figures are stated as of November 2026.

← All posts

Ask AI about this article

Ask a question about this article — ASI Biont will answer based on its text.