swiftui-patterns
Skill này làm gì
Hướng dẫn này dạy các thực hành tốt nhất để xây dựng ứng dụng iPhone và Mac hiện đại bằng SwiftUI. Nó giải thích cách tổ chức code, quản lý dữ liệu và làm cho điều hướng hoạt động hiệu quả.
Khi nào nên dùng
- ✓Khi bắt đầu một ứng dụng SwiftUI mới.
- ✓Khi cố gắng tổ chức code ứng dụng đang có.
- ✓Khi ứng dụng chạy chậm hoặc không phản hồi nhanh.
Liên kết skill
https://github.com/affaan-m/ECC/blob/main/pi/core/skills/swiftui-patterns/SKILL.mdSao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.
Cách dùng
Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.
Tin nhắn có sẵn để dán
Dành cho Claude Code, Codex hoặc Cursor: chạy lệnh trong thư mục dự án của bạn và skill sẽ được cài đặt.
Tải tệp SKILL.md về và thả vào thư mục skill của agent.
Ví dụ cách nhờ AI
“Làm sao để tổ chức code SwiftUI của tôi tốt hơn cho việc quản lý dữ liệu và điều hướng?”
5 skill mới mỗi tuần. Đọc chỉ 1 phút.
Cách hoạt động
Nó chỉ cách dùng các công cụ SwiftUI cụ thể như `@Observable` để quản lý dữ liệu và `NavigationStack` để điều hướng. Mục tiêu là làm code gọn gàng hơn và ứng dụng nhanh hơn.
Nội dung kỹ thuật (SKILL.md gốc)
Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.
SwiftUI Patterns
Modern SwiftUI patterns for building declarative, performant user interfaces on Apple platforms. Covers the Observation framework, view composition, type-safe navigation, and performance optimization.
When to Activate
- Building SwiftUI views and managing state (
@State,@Observable,@Binding) - Designing navigation flows with
NavigationStack - Structuring view models and data flow
- Optimizing rendering performance for lists and complex layouts
- Working with environment values and dependency injection in SwiftUI
State Management
Property Wrapper Selection
Choose the simplest wrapper that fits:
| Wrapper | Use Case |
|---|---|
@State |
View-local value types (toggles, form fields, sheet presentation) |
@Binding |
Two-way reference to parent's @State |
@Observable class + @State |
Owned model with multiple properties |
@Observable class (no wrapper) |
Read-only reference passed from parent |
@Bindable |
Two-way binding to an @Observable property |
@Environment |
Shared dependencies injected via .environment() |
@Observable ViewModel
Use @Observable (not ObservableObject) — it tracks property-level changes so SwiftUI only re-renders views that read the changed property:
@Observable
final class ItemListViewModel {
private(set) var items: [Item] = []
private(set) var isLoading = false
var searchText = ""
private let repository: any ItemRepository
init(repository: any ItemRepository = DefaultItemRepository()) {
self.repository = repository
}
func load() async {
isLoading = true
defer { isLoading = false }
items = (try? await repository.fetchAll()) ?? []
}
}
View Consuming the ViewModel
struct ItemListView: View {
@State private var viewModel: ItemListViewModel
init(viewModel: ItemListViewModel = ItemListViewModel()) {
_viewModel = State(initialValue: viewModel)
}
var body: some View {
List(viewModel.items) { item in
ItemRow(item: item)
}
.searchable(text: $viewModel.searchText)
.overlay { if viewModel.isLoading { ProgressView() } }
.task { await viewModel.load() }
}
}
Environment Injection
Replace @EnvironmentObject with @Environment:
// Inject
ContentView()
.environment(authManager)
// Consume
struct ProfileView: View {
@Environment(AuthManager.self) private var auth
var body: some View {
Text(auth.currentUser?.name ?? "Guest")
}
}
View Composition
Extract Subviews to Limit Invalidation
Break views into small, focused structs. When state changes, only the subview reading that state re-renders:
struct OrderView: View {
@State private var viewModel = OrderViewModel()
var body: some View {
VStack {
OrderHeader(title: viewModel.title)
OrderItemList(items: viewModel.items)
OrderTotal(total: viewModel.total)
}
}
}
ViewModifier for Reusable Styling
struct CardModifier: ViewModifier {
func body(content: Content) -> some View {
content
.padding()
.background(.regularMaterial)
.clipShape(RoundedRectangle(cornerRadius: 12))
}
}
extension View {
func cardStyle() -> some View {
modifier(CardModifier())
}
}
Navigation
Type-Safe NavigationStack
Use NavigationStack with NavigationPath for programmatic, type-safe routing:
@Observable
final class Router {
var path = NavigationPath()
func navigate(to destination: Destination) {
path.append(destination)
}
func popToRoot() {
path = NavigationPath()
}
}
enum Destination: Hashable {
case detail(Item.ID)
case settings
case profile(User.ID)
}
struct RootView: View {
@State private var router = Router()
var body: some View {
NavigationStack(path: $router.path) {
HomeView()
.navigationDestination(for: Destination.self) { dest in
switch dest {
case .detail(let id): ItemDetailView(itemID: id)
case .settings: SettingsView()
case .profile(let id): ProfileView(userID: id)
}
}
}
.environment(router)
}
}
Performance
…
Câu hỏi thường gặp
Khác nhau giữa `@State` và `@Observable` là gì?
`@State` dùng cho dữ liệu đơn giản thuộc về một màn hình. `@Observable` dùng cho dữ liệu phức tạp hơn, có thể chia sẻ và quản lý hiệu quả hơn, chỉ cập nhật những phần màn hình thực sự cần vẽ lại.
Làm sao để điều hướng màn hình vững chắc hơn?
Dùng `NavigationStack` với `NavigationPath`. Cách này cho phép bạn xác định rõ nơi có thể đi tới và với dữ liệu nào, tránh các lỗi điều hướng thường gặp.
Vì sao ứng dụng của tôi chạy chậm?
Có thể các phần không cần thiết của màn hình đang bị vẽ lại. Hãy dùng `LazyVStack` cho danh sách lớn và chia màn hình thành các thành phần nhỏ hơn, để chỉ phần cần thiết được cập nhật.
Chi tiết
Mô tả gốc của tác giả
SwiftUI architecture patterns, state management with @Observable, view composition, navigation, performance optimization, and modern iOS/macOS UI best practices. Use when building or reviewing SwiftUI views, @Observable state, navigation, or render performance.
repo: affaan-m/ECC
Độ phổ biến
- Sao GitHub
- 280k
- Vị trí xếp hạng
- #252
- Giấy phép
- MIT
- Cập nhật
- 15/10/2026
Bình luận
- …
Thêm skill từ kho mã này
accessibility
280kGiúp agent của bạn tạo giao diện mà ai cũng dùng được, đảm bảo website và app hoạt động tốt với người khuyết tật như người mù hoặc khó vận động.
agent-architecture-audit
280kGiúp agent của bạn kiểm tra xem mọi thứ có hoạt động đúng không, đồng thời tìm và sửa các lỗi trong hệ thống AI để chúng chạy như mong đợi.
agent-eval
280kGiúp agent của bạn so sánh các công cụ lập trình AI khác nhau, đo xem công cụ nào nhanh hơn, rẻ hơn và đáng tin cậy hơn cho từng tác vụ.
agent-harness-construction
280kGiúp agent của bạn hiểu rõ hơn cách thực hiện tác vụ, tối ưu các công cụ và chỉ dẫn mà agent dùng để tăng khả năng thành công.
Skill tương tự
dream-loop
260kGiúp agent của bạn đẩy một cảnh 3D hoặc ứng dụng trực quan lên chất lượng đồ họa cao, bằng cách so ảnh chụp màn hình với concept art và lặp lại.
pretext
260kXây dựng các bản demo trình duyệt vui nhộn, trong đó chữ tự chảy quanh các hình chuyển động, tạo màn chơi hoặc tách thành các hạt.
page-agent
260kGiúp agent của bạn thêm một trợ lý AI tích hợp vào ứng dụng web, để người dùng ra lệnh bằng ngôn ngữ thường và trang thực hiện theo.