Expert in iOS development with Swift, SwiftUI, UIKit, Combine, async/await, and Apple platform best practices. Use for building iOS apps, widgets, extensions, and Apple ecosystem integrations.
Provide expert-level iOS development assistance focusing on Swift, SwiftUI, UIKit, app architecture, performance optimization, and Apple platform best practices.
import SwiftUI
struct ContentView: View {
@StateObject private var viewModel = ContentViewModel()
@Environment(\.dismiss) private var dismiss
var body: some View {
NavigationStack {
List(viewModel.items) { item in
ItemRow(item: item)
}
.navigationTitle("Items")
.toolbar {
ToolbarItem(placement: .primaryAction) {
Button("Add", systemImage: "plus") {
viewModel.addItem()
}
}
}
.task {
await viewModel.loadItems()
}
.refreshable {
await viewModel.loadItems()
}
.alert("Error", isPresented: $viewModel.showError) {
Button("OK") {}
} message: {
Text(viewModel.errorMessage)
}
}
}
}
#Preview {
ContentView()
}
import Foundation
import Observation
@Observable
final class ContentViewModel {
var items: [Item] = []
var isLoading = false
var showError = false
var errorMessage = ""
private let repository: ItemRepositoryProtocol
init(repository: ItemRepositoryProtocol = ItemRepository()) {
self.repository = repository
}
func loadItems() async {
isLoading = true
defer { isLoading = false }
do {
items = try await repository.fetchItems()
} catch {
errorMessage = error.localizedDescription
showError = true
}
}
func addItem() {
let newItem = Item(title: "New Item", createdAt: .now)
items.append(newItem)
}
}
import Foundation
protocol APIClientProtocol {
func request<T: Decodable>(_ endpoint: Endpoint) async throws -> T
}
struct APIClient: APIClientProtocol {
private let session: URLSession
private let decoder: JSONDecoder
init(session: URLSession = .shared) {
self.session = session
self.decoder = JSONDecoder()
self.decoder.dateDecodingStrategy = .iso8601
self.decoder.keyDecodingStrategy = .convertFromSnakeCase
}
func request<T: Decodable>(_ endpoint: Endpoint) async throws -> T {
let (data, response) = try await session.data(for: endpoint.urlRequest)
guard let httpResponse = response as? HTTPURLResponse else {
throw APIError.invalidResponse
}
guard (200...299).contains(httpResponse.statusCode) else {
throw APIError.httpError(statusCode: httpResponse.statusCode)
}
return try decoder.decode(T.self, from: data)
}
}
enum APIError: LocalizedError {
case invalidResponse
case httpError(statusCode: Int)
var errorDescription: String? {
switch self {
case .invalidResponse: "Invalid server response"
case .httpError(let code): "Server error (HTTP \(code))"
}
}
}
import SwiftData
@Model
final class Task {
var title: String
var isCompleted: Bool
var createdAt: Date
init(title: String, isCompleted: Bool = false) {
self.title = title
self.isCompleted = isCompleted
self.createdAt = .now
}
}
// In App
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
.modelContainer(for: Task.self)
}
}
// In View
struct TaskListView: View {
@Query(sort: \Task.createdAt, order: .reverse) var tasks: [Task]
@Environment(\.modelContext) private var context
var body: some View {
List(tasks) { task in
TaskRow(task: task)
}
}
}
enum Route: Hashable {
case detail(Item)
case settings
case profile(userId: String)
}
struct RootView: View {
@State private var path = NavigationPath()
var body: some View {
NavigationStack(path: $path) {
HomeView()
.navigationDestination(for: Route.self) { route in
switch route {
case .detail(let item): DetailView(item: item)
case .settings: SettingsView()
case .profile(let id): ProfileView(userId: id)
}
}
}
}
}
private struct RepositoryKey: EnvironmentKey {
static let defaultValue: ItemRepositoryProtocol = ItemRepository()
}
extension EnvironmentValues {
var itemRepository: ItemRepositoryProtocol {
get { self[RepositoryKey.self] }
set { self[RepositoryKey.self] = newValue }
}
}
// Usage in views
struct ItemListView: View {
@Environment(\.itemRepository) private var repository
}
enum AppError: LocalizedError {
case networkUnavailable
case unauthorized
case notFound
case serverError(underlying: Error)
var errorDescription: String? {
switch self {
case .networkUnavailable: "No internet connection"
case .unauthorized: "Please sign in again"
case .notFound: "Content not found"
case .serverError: "Something went wrong"
}
}
var recoverySuggestion: String? {
switch self {
case .networkUnavailable: "Check your connection and try again"
case .unauthorized: "Your session has expired"
default: "Please try again later"
}
}
}
Project Organization
Performance
Testing
Accessibility
App Lifecycle
When implementing iOS features:
This skill ensures production-ready, maintainable, and performant iOS applications following Apple's latest guidelines and modern Swift patterns.