154 lines
4.1 KiB
Markdown
154 lines
4.1 KiB
Markdown
# iOS Development Code Examples
|
|
|
|
Complete implementation examples for common iOS and macOS development patterns.
|
|
|
|
## SwiftUI View with Proper State Management
|
|
|
|
```swift
|
|
import SwiftUI
|
|
|
|
struct UserProfileView: View {
|
|
@StateObject private var viewModel = UserProfileViewModel()
|
|
@Environment(\.dismiss) private var dismiss
|
|
|
|
var body: some View {
|
|
NavigationStack {
|
|
ScrollView {
|
|
VStack(alignment: .leading, spacing: 16) {
|
|
// Profile content
|
|
AsyncImage(url: viewModel.avatarURL) { image in
|
|
image
|
|
.resizable()
|
|
.scaledToFill()
|
|
} placeholder: {
|
|
ProgressView()
|
|
}
|
|
.frame(width: 100, height: 100)
|
|
.clipShape(Circle())
|
|
|
|
Text(viewModel.userName)
|
|
.font(.title)
|
|
.accessibilityAddTraits(.isHeader)
|
|
}
|
|
.padding()
|
|
}
|
|
.navigationTitle("Profile")
|
|
.toolbar {
|
|
ToolbarItem(placement: .navigationBarTrailing) {
|
|
Button("Done") {
|
|
dismiss()
|
|
}
|
|
}
|
|
}
|
|
.task {
|
|
await viewModel.loadProfile()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
```
|
|
|
|
## MVVM ViewModel with Async/Await
|
|
|
|
```swift
|
|
import Foundation
|
|
import Combine
|
|
|
|
@MainActor
|
|
final class UserProfileViewModel: ObservableObject {
|
|
@Published private(set) var userName: String = ""
|
|
@Published private(set) var avatarURL: URL?
|
|
@Published private(set) var isLoading = false
|
|
@Published private(set) var error: Error?
|
|
|
|
private let userService: UserServiceProtocol
|
|
|
|
init(userService: UserServiceProtocol = UserService()) {
|
|
self.userService = userService
|
|
}
|
|
|
|
func loadProfile() async {
|
|
isLoading = true
|
|
error = nil
|
|
|
|
do {
|
|
let profile = try await userService.fetchCurrentUser()
|
|
userName = profile.name
|
|
avatarURL = profile.avatarURL
|
|
} catch {
|
|
self.error = error
|
|
}
|
|
|
|
isLoading = false
|
|
}
|
|
}
|
|
|
|
// Protocol for dependency injection and testing
|
|
protocol UserServiceProtocol {
|
|
func fetchCurrentUser() async throws -> UserProfile
|
|
}
|
|
```
|
|
|
|
## Core Data Stack with Modern Concurrency
|
|
|
|
```swift
|
|
import CoreData
|
|
|
|
final class PersistenceController {
|
|
static let shared = PersistenceController()
|
|
|
|
let container: NSPersistentContainer
|
|
|
|
private init() {
|
|
container = NSPersistentContainer(name: "AppModel")
|
|
container.loadPersistentStores { description, error in
|
|
if let error = error {
|
|
fatalError("Unable to load persistent stores: \(error)")
|
|
}
|
|
}
|
|
container.viewContext.automaticallyMergesChangesFromParent = true
|
|
}
|
|
|
|
func save() async throws {
|
|
let context = container.viewContext
|
|
guard context.hasChanges else { return }
|
|
|
|
try await context.perform {
|
|
try context.save()
|
|
}
|
|
}
|
|
|
|
func backgroundContext() -> NSManagedObjectContext {
|
|
let context = container.newBackgroundContext()
|
|
context.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
|
|
return context
|
|
}
|
|
}
|
|
```
|
|
|
|
## Proper Memory Management with Closures
|
|
|
|
```swift
|
|
import UIKit
|
|
|
|
final class DataManager {
|
|
private var completionHandlers: [String: (Result<Data, Error>) -> Void] = [:]
|
|
|
|
func fetchData(forKey key: String, completion: @escaping (Result<Data, Error>) -> Void) {
|
|
completionHandlers[key] = completion
|
|
|
|
URLSession.shared.dataTask(with: URL(string: "https://example.com")!) { [weak self] data, response, error in
|
|
guard let self = self else { return }
|
|
|
|
if let error = error {
|
|
self.completionHandlers[key]?(.failure(error))
|
|
} else if let data = data {
|
|
self.completionHandlers[key]?(.success(data))
|
|
}
|
|
|
|
self.completionHandlers[key] = nil
|
|
}.resume()
|
|
}
|
|
}
|
|
```
|