Counter With Alert - Swiftui Typing CST Test
Loading…
Counter With Alert — Swiftui Code
Counter shows an alert when it reaches 10.
import SwiftUI
struct AlertCounterView: View {
@State private var count: Int = 0
@State private var showAlert: Bool = false
var body: some View {
VStack(spacing: 20) {
Text("Counter: \(count)")
.font(.largeTitle)
HStack {
Button("+") {
count += 1
if count == 10 { showAlert = true }
}
Button("Reset") { count = 0 }
}
}.alert(isPresented: $showAlert) {
Alert(title: Text("Alert"), message: Text("Count reached 10!"), dismissButton: .default(Text("OK")))
}.padding()
}
}
@main
struct AlertCounterApp: App {
var body: some Scene {
WindowGroup {
AlertCounterView()
}
}
}Swiftui Language Guide
SwiftUI is Apple’s declarative framework for building user interfaces across iOS, macOS, watchOS, and tvOS using Swift. It allows developers to create fully native apps with less code and real-time previews.
Primary Use Cases
- ▸Native iOS apps with modern UI
- ▸macOS desktop apps
- ▸watchOS and tvOS apps
- ▸Rapid prototyping for Apple platforms
- ▸Apps leveraging Swift ecosystem and Apple frameworks
Notable Features
- ▸Declarative syntax for UI building
- ▸Live preview in Xcode
- ▸Cross-Apple-platform support
- ▸Seamless integration with Swift and Combine
- ▸Supports animations, gestures, and adaptive layouts
Origin & Creator
Created by Apple in 2019, SwiftUI was introduced to unify UI development across Apple platforms and replace UIKit/AppKit with a declarative approach.
Industrial Note
Perfect for Apple ecosystem apps where full native performance, Swift language, and declarative UI are priorities.
Quick Explain
- ▸SwiftUI enables building native Apple apps using a declarative syntax.
- ▸It integrates seamlessly with Swift and Apple’s ecosystem, providing live previews in Xcode.
- ▸Ideal for developers focusing on iOS/macOS apps who want fast, maintainable, and modern UI development.
Core Features
- ▸Swift-based declarative UI components
- ▸State-driven rendering with @State, @Binding, @ObservedObject
- ▸Integration with Combine for reactive programming
- ▸Support for Dark Mode, localization, accessibility
- ▸Previews and canvas in Xcode for real-time feedback
Learning Path
- ▸Learn Swift language basics
- ▸Understand SwiftUI declarative syntax
- ▸Explore property wrappers and state management
- ▸Build small projects and use Xcode previews
- ▸Deploy apps to iOS/macOS devices
Practical Examples
- ▸iOS task manager app
- ▸macOS file manager utility
- ▸watchOS fitness tracking app
- ▸tvOS streaming app UI
- ▸Prototype interactive app with SwiftUI previews
Comparisons
- ▸SwiftUI vs UIKit: declarative vs imperative UI building
- ▸SwiftUI vs React Native: native Apple-only vs cross-platform JS
- ▸SwiftUI offers seamless integration with Apple ecosystem
- ▸UIKit may still be needed for complex legacy components
- ▸SwiftUI emphasizes speed, maintainability, and live previews
Strengths
- ▸Fully native performance
- ▸Single codebase across Apple platforms
- ▸Minimal boilerplate code with declarative approach
- ▸Tight integration with Swift and Apple frameworks
- ▸Live preview and hot reload speeds up development
Limitations
- ▸Apple platforms only, not cross-platform outside Apple
- ▸Requires Swift knowledge
- ▸Some complex UI components require UIKit fallback
- ▸Limited backward compatibility (iOS 13+)
- ▸Smaller ecosystem compared to UIKit/React Native for some features
When NOT to Use
- ▸Apps targeting Android or cross-platform
- ▸Complex UI requiring extensive UIKit components
- ▸Teams not familiar with Swift
- ▸Projects requiring iOS < 13 support
- ▸Highly customized animations not yet supported in SwiftUI
Cheat Sheet
- ▸`@State var name: String` - reactive state variable
- ▸`Text(name)` - display reactive value
- ▸`VStack { ... }` - vertical layout container
- ▸`List(data) { item in ... }` - dynamic list
- ▸`.animation(.default)` - add animation to view
FAQ
- ▸Is SwiftUI maintained?
- ▸Yes, by Apple and continuously updated with Xcode.
- ▸Can I use SwiftUI for macOS apps?
- ▸Yes, it supports iOS, macOS, watchOS, and tvOS.
- ▸Does it support animations?
- ▸Yes, built-in declarative animations and gestures.
- ▸Can I mix SwiftUI and UIKit?
- ▸Yes, use UIHostingController and UIViewRepresentable.
- ▸Is SwiftUI suitable for production apps?
- ▸Yes, widely used in modern Apple apps.
30-Day Skill Plan
- ▸Week 1: Learn Swift and create basic SwiftUI views
- ▸Week 2: Use @State, @Binding, @ObservedObject for dynamic UI
- ▸Week 3: Add animations and gestures
- ▸Week 4: Integrate Combine and networking
- ▸Week 5: Optimize, test, and deploy app to device
Final Summary
- ▸SwiftUI is Apple’s modern declarative UI framework for native apps.
- ▸Supports iOS, macOS, watchOS, and tvOS from a single Swift codebase.
- ▸Integrates seamlessly with Swift, Combine, and Apple ecosystem.
- ▸Enables fast, maintainable, and visually dynamic apps.
- ▸Focuses on reactive state-driven UI and live development previews.
Project Structure
- ▸App/ - main entry point and app lifecycle
- ▸Views/ - reusable SwiftUI view components
- ▸Models/ - data models
- ▸Assets/ - images, colors, fonts
- ▸Supporting files - Info.plist, AppDelegate, SceneDelegate
Monetization
- ▸Consumer apps via App Store
- ▸In-app purchases and subscriptions
- ▸Enterprise apps for iOS/macOS
- ▸Consulting for SwiftUI development
- ▸Rapid prototyping and deployment for startups
Productivity Tips
- ▸Leverage Xcode previews for rapid iteration
- ▸Use reusable SwiftUI views and modifiers
- ▸Apply Combine for asynchronous tasks
- ▸Test on multiple devices frequently
- ▸Document state management and view hierarchy
Basic Concepts
- ▸Views are Swift structs conforming to `View` protocol
- ▸Use @State, @Binding, @ObservedObject for reactive state
- ▸Modifiers change view properties (e.g., `.padding()`, `.background()`)
- ▸Combine used for asynchronous/reactive updates
- ▸Previews provide real-time feedback in Xcode canvas