1. ์ฌ์ ์๊ฑด & ์์คํ ์๊ตฌ์ฌ์ (Prerequisites & System Requirements)
๐ป ์์คํ ์ค์น ๋ฐ ์คํ ์๊ตฌ์ฌ์ (System Requirements)
| ํญ๋ชฉ | ์ต์ ์ฌ์ (Minimum) | ๊ถ์ฅ ์ฌ์ (Recommended) | ์ฐธ๊ณ ๋ฐ ๋น๊ณ |
|---|---|---|---|
| ์ด์์ฒด์ (OS) | macOS Sonoma 14.5 ์ด์ | macOS Sequoia 15.x+ (์ต์ ์ ์ง ๊ถ์ฅ) | iOS/iPadOS/macOS ๋ค์ดํฐ๋ธ ๋น๋์๋ Mac ํ๊ฒฝ ํ์ |
| CPU | Apple Silicon M1 (8์ฝ์ด) ๋๋ Intel Core i7 8์ธ๋+ | Apple Silicon M2 / M3 / M4 Pro/Max (10~14์ฝ์ด ์ด์) | Swift ๋งคํฌ๋ก ํ์ฅ ๋ฐ SPM ์์กด์ฑ ๋ณ๋ ฌ ์ปดํ์ผ ๊ฐ์ |
| ๋ฉ๋ชจ๋ฆฌ (RAM) | 16 GB ์ด์ | 24 GB ~ 36 GB ์ด์ (Apple Silicon ํตํฉ ๋ฉ๋ชจ๋ฆฌ) | Xcode + ์๋ฎฌ๋ ์ดํฐ(Simulator) + Canvas ์ค์๊ฐ ํ๋ฆฌ๋ทฐ ๋์ ๊ตฌ๋ |
| ๋์คํฌ (Storage) | 40 GB ์ด์์ ์ฌ์ ๊ณต๊ฐ | 100 GB ์ด์์ ๊ณ ์ NVMe/SSD | Xcode ์ฑ ์์ฒด(์ฝ 30GB |
| ๊ฐ๋ฐ ๋๊ตฌ / ๋ฐํ์ | Xcode 16.0+, Command Line Tools, Git | Xcode 16.2+, Swift 6.0 / 6.1+, Homebrew, SwiftLint | iOS 18+ / macOS 15+ ํ๊น SDK ์ง์ |
๐ ๏ธ ๊ฐ๋ฐ ํ๊ฒฝ ๊ตฌ์ถ & CLI ํ์ ๋ช ๋ น์ด
# =============================================================
# 1. Xcode ๋ช
๋ น์ค ๋๊ตฌ(Command Line Tools) ์ค์น ๋ฐ ํ์ธ
# =============================================================
xcode-select --install
# ํ์ฑํ๋ ๊ฐ๋ฐ์ ๋๋ ํฐ๋ฆฌ ๊ฒฝ๋ก ํ์ธ (Xcode.app ๋ด๋ถ ๊ฒฝ๋ก ์ง์ ํ์)
xcode-select -p
# /Applications/Xcode.app/Contents/Developer
# ๋ง์ฝ Command Line Tools ๊ฒฝ๋ก๋ก ์๋ชป ์กํ ์๋ค๋ฉด ์ ํ:
sudo xcode-select -s /Applications/Xcode.app/Contents/Developer
# =============================================================
# 2. Swift ์ปดํ์ผ๋ฌ ๋ฒ์ ๋ฐ ํด์ฒด์ธ ํ์ธ
# =============================================================
swift --version
# Apple Swift version 6.0 (swiftlang-6.0.0.9.10 clang-1600.0.26.2)
# Target: arm64-apple-macosx15.0
# =============================================================
# 3. ์ ์ฉํ ๋ณด์กฐ ๋๊ตฌ ์ค์น (Homebrew)
# =============================================================
# ์ฝ๋ ์ปจ๋ฒค์
๋ฆฐํฐ & ํฌ๋งคํฐ
brew install swiftlint
brew install swift-format
# Xcode ๋น๋ ๋ก๊ทธ๋ฅผ ๊น๋ํ๊ฒ ์ถ๋ ฅํด์ฃผ๋ ๋๊ตฌ
brew install xcbeautify
# =============================================================
# 4. ์๋ฎฌ๋ ์ดํฐ ๋ชฉ๋ก ํ์ธ ๋ฐ CLI ์คํ
# =============================================================
# ์ฌ์ฉ ๊ฐ๋ฅํ iOS ๋ฐํ์ ๋ฐ ๊ธฐ๊ธฐ UUID ์กฐํ
xcrun simctl list devices available
# ํน์ ์๋ฎฌ๋ ์ดํฐ ๋ถํ
(iPhone 16)
xcrun simctl boot "iPhone 16"
# ์๋ฎฌ๋ ์ดํฐ GUI ์ฐฝ ๋์ฐ๊ธฐ
open -a Simulator
2. ํต์ฌ ๋ฉํ ๋ชจ๋ธ (Declarative UI & Data Flow)
SwiftUI๋ ์ ์ธํ(Declarative) UI ํ๋ ์์ํฌ์ ๋๋ค. ๊ฐ๋ฐ์๊ฐ โํ๋ฉด์ ์ด๋ป๊ฒ ๊ทธ๋ฆด์ง(How)โ ๋ช ๋ นํ๋ ๋์ , โํ์ฌ ๋ฐ์ดํฐ ์ํ์ ๋ฐ๋ผ ํ๋ฉด์ด ์ด๋ป๊ฒ ๋ณด์ฌ์ผ ํ๋์ง(What)โ ์ ์ธํฉ๋๋ค.
๐ ์ํ์ ๋ทฐ์ ๋จ๋ฐฉํฅ ๋ฐ์ดํฐ ํ๋ฆ (Single Source of Truth)
+-----------------------------------------------------------+
| ๋ฐ์ดํฐ ์ํ (State) |
| @State, @Binding, @Observable ํด๋์ค, @Environment |
+-----------------------------+-----------------------------+
โ
(1) ์ํ ๋ณ๊ฒฝ ๋ฐ์ โ (2) ๋ณ๊ฒฝ ๊ตฌ๋
๋ฐ Diff ์ฐ์ฐ
โผ
+-----------------------------------------------------------+
| SwiftUI View ๊ณ์ธต (๊ตฌ์กฐ์ฒด Struct) |
| var body: some View { ... } ์์ ํจ์์ฒ๋ผ ํ๊ฐ |
+-----------------------------+-----------------------------+
โ
โผ (3) ํ๋ฉด ๋ ๋๋ง (UIKit/AppKit ๋ค์ดํฐ๋ธ ๋ ๋๋ฌ)
+-----------------------------------------------------------+
| ์ฌ์ฉ์ ์ธํฐํ์ด์ค (Screen / UI) |
| ์ฌ์ฉ์ ์ํธ์์ฉ (๋ฒํผ ํด๋ฆญ, ํ
์คํธ ์
๋ ฅ, ์ค์์ดํ ๋ฑ) |
+-----------------------------+-----------------------------+
โ
โผ (4) Action ์ด๋ฒคํธ ๋ฐ์ -> ์ํ ๋ณ๊ฒฝ
๐ ๋ชจ๋ ์ํ ๊ด๋ฆฌ ์ด๋ ธํ ์ด์ ๋น๊ต (iOS 17+ ์ ๊ท ํ์ค)
| ํ๋กํผํฐ ๋ํผ / ๋งคํฌ๋ก | ํ์ ์ข ๋ฅ | ์์ ๊ถ (Ownership) | ์ฃผ์ ์ฉ๋ ๋ฐ ํน์ง |
|---|---|---|---|
@State | ๊ฐ ํ์
(struct, ๊ธฐ๋ณธํ) | ๋ทฐ๊ฐ ์ง์ ์์ (Private) | ํด๋น ๋ทฐ ๋ด๋ถ์์๋ง ์๋ช ์ฃผ๊ธฐ๋ฅผ ๊ด๋ฆฌํ๋ ๋จ์ ๋ก์ปฌ UI ์ํ |
@Binding | ๊ฐ ํ์ ์ฐธ์กฐ ์ ๋ฌ | ๋ถ๋ชจ ๋ทฐ๊ฐ ์์ | ๋ถ๋ชจ์ @State๋ฅผ ์์ ๋ทฐ๋ก ์ ๋ฌํ์ฌ ์๋ฐฉํฅ ๋๊ธฐํ |
@Observable (์ ๊ท) | ์ฐธ์กฐ ํ์
(class) | ์ธ์คํด์ค ์์ | iOS 17+ ๊ถ์ฅ ํ์ค. ์์ฑ๋ณ ์๋ ์์กด์ฑ ์ถ์ , @Published ๋ถํ์ |
@Environment | ์์คํ ๋๋ ์ปค์คํ ๊ฐ์ฒด | ๋ทฐ ๊ณ์ธต ์ ์ฒด ๊ณต์ | ๋คํฌ๋ชจ๋(colorScheme), ํ๋ฉด ๋ซ๊ธฐ(dismiss), ์ ์ญ ์์กด์ฑ ์ฃผ์
|
ObservableObject (๋ ๊ฑฐ์) | ์ฐธ์กฐ ํ์
(class ํ๋กํ ์ฝ) | @StateObject ์์ | iOS 16 ์ดํ ํธํ์ฉ. @Published ์์ฑ ๋ณ๊ฒฝ ์ ๋ทฐ ์ ์ฒด ๋ฌดํจํ |
3. [์ด๋ณด์ 1๋จ๊ณ] ํฌ๋ก์๋ / ์ต์ ๋์ ์์
๋ณต์ฌํ์ฌ Xcode์ ContentView.swift ๋๋ ์ ๊ท SwiftUI ํ๋ก์ ํธ์ ๋ฐ๋ก ๋ถ์ฌ๋ฃ์ด ์คํํ ์ ์๋ ๋
๋ฆฝํ ์์ ์์ ์
๋๋ค.
import SwiftUI
// MARK: - [Step 1] ์ฑ์ ์ง์
์ (App Protocol)
@main
struct HelloWorldApp: App {
var body: some Scene {
WindowGroup {
CounterContentView()
}
}
}
// MARK: - [Step 2] ๋ฉ์ธ ๋ทฐ ๊ตฌํ
struct CounterContentView: View {
// ๋ทฐ๊ฐ ์์ ํ๋ ๋จ์ผ ์ง์ค ๊ณต๊ธ์(Single Source of Truth) ๋ก์ปฌ ์ํ
@State private var count: Int = 0
@State private var isLiked: Bool = false
var body: some View {
VStack(spacing: 24) {
// ํค๋ ํ์ดํ ๋ฐ SF Symbols ์์ด์ฝ
HStack(spacing: 8) {
Image(systemName: "apple.logo")
.font(.system(size: 32))
.foregroundStyle(.primary)
Text("SwiftUI ์์ํ๊ธฐ")
.font(.title2)
.fontWeight(.bold)
}
// ์นด์ดํฐ ๋์คํ๋ ์ด ์นด๋
VStack(spacing: 12) {
Text("ํ์ฌ ์นด์ดํธ")
.font(.subheadline)
.foregroundStyle(.secondary)
Text("\(count)")
.font(.system(size: 56, weight: .heavy, design: .rounded))
.foregroundStyle(count >= 0 ? Color.blue : Color.red)
.contentTransition(.numericText()) // ์ซ์ ๋ณ๊ฒฝ ์ ๋ถ๋๋ฌ์ด ์ ๋๋ฉ์ด์
}
.frame(maxWidth: .infinity)
.padding(.vertical, 32)
.background(Color(.secondarySystemBackground))
.clipShape(RoundedRectangle(cornerRadius: 16))
// ์ปจํธ๋กค ๋ฒํผ ์ธํธ
HStack(spacing: 16) {
Button(action: {
withAnimation(.spring(response: 0.3, dampingFraction: 0.6)) {
count -= 1
}
}) {
Label("๊ฐ์", systemImage: "minus.circle.fill")
.font(.headline)
.frame(maxWidth: .infinity)
.padding()
.background(Color.red.opacity(0.15))
.foregroundStyle(.red)
.clipShape(RoundedRectangle(cornerRadius: 12))
}
Button(action: {
withAnimation(.spring(response: 0.3, dampingFraction: 0.6)) {
count += 1
}
}) {
Label("์ฆ๊ฐ", systemImage: "plus.circle.fill")
.font(.headline)
.frame(maxWidth: .infinity)
.padding()
.background(Color.blue)
.foregroundStyle(.white)
.clipShape(RoundedRectangle(cornerRadius: 12))
}
}
// ํ ๊ธ ๋ฒํผ ๋ฐ ์กฐ๊ฑด๋ถ ๋ ๋๋ง
Button(action: {
withAnimation {
isLiked.toggle()
}
}) {
HStack {
Image(systemName: isLiked ? "heart.fill" : "heart")
.foregroundStyle(isLiked ? .pink : .secondary)
Text(isLiked ? "์ฆ๊ฒจ์ฐพ๊ธฐ ๋ฑ๋ก๋จ" : "์ฆ๊ฒจ์ฐพ๊ธฐ ์ถ๊ฐ")
.font(.subheadline)
.foregroundStyle(isLiked ? .pink : .primary)
}
.padding(.horizontal, 16)
.padding(.vertical, 10)
.background(isLiked ? Color.pink.opacity(0.12) : Color(.tertiarySystemBackground))
.clipShape(Capsule())
}
Spacer()
}
.padding(24)
}
}
// MARK: - [Step 3] Xcode ์ค์๊ฐ ์บ๋ฒ์ค ํ๋ฆฌ๋ทฐ (iOS 17+ ๋งคํฌ๋ก)
#Preview("๋ผ์ดํธ ๋ชจ๋") {
CounterContentView()
}
#Preview("๋คํฌ ๋ชจ๋") {
CounterContentView()
.preferredColorScheme(.dark)
}
[์คํ ๊ฒฐ๊ณผ ์ฝ์ & UI ๋์]
1. ํ๋ฉด ์๋จ: Apple ๋ก๊ณ ์ ํจ๊ป "SwiftUI ์์ํ๊ธฐ" ๋ณผ๋ ํ์ดํ ๋ ๋๋ง.
2. ์ค์ ์นด๋: 56pt ๋ผ์ด๋ ๋ณผ๋ ํฐํธ๋ก 0 ํ์.
3. [์ฆ๊ฐ] ํญ ์: ์คํ๋ง ๋ฌผ๋ฆฌ ๋ฐ์ด์ค์ ํจ๊ป ์ซ์๊ฐ 1, 2, 3...์ผ๋ก ๋ถ๋๋ฝ๊ฒ ์ฆ๊ฐ.
4. [ํํธ ๋ฒํผ] ํญ ์: ๋ถํ์ ์ฑ์์ง ํํธ๋ก ์ ๋๋ฉ์ด์
๋๋ฉฐ "์ฆ๊ฒจ์ฐพ๊ธฐ ๋ฑ๋ก๋จ"์ผ๋ก ์ํ ์ฆ์ ๋ฐ์.
4. [2๋จ๊ณ] ํต์ฌ ๊ธฐ๋ฅ ๊ฐ์ด๋
4.1 ๊ธฐ๋ณธ ๋ ์ด์์ ์ปจํ ์ด๋ (Stacks & ScrollView)
SwiftUI๋ ํฌ๊ฒ 3๊ฐ์ง์ ์ ํ ์คํ๊ณผ 2๊ฐ์ง ์ง์ฐ ๋ก๋ฉ ์ปจํ ์ด๋๋ฅผ ๊ธฐ๋ณธ ์ ๊ณตํฉ๋๋ค.
import SwiftUI
struct LayoutDemoView: View {
let columns = [
GridItem(.flexible(), spacing: 12),
GridItem(.flexible(), spacing: 12)
]
var body: some View {
ScrollView(.vertical, showsIndicators: true) {
VStack(alignment: .leading, spacing: 20) {
// 1. HStack: ์ํ ์ ๋ ฌ
HStack(alignment: .center, spacing: 12) {
Circle()
.fill(Color.orange)
.frame(width: 44, height: 44)
VStack(alignment: .leading, spacing: 4) {
Text("์ฌ์ฉ์ ํ๋กํ")
.font(.headline)
Text("ios_developer@luckytech.com")
.font(.caption)
.foregroundStyle(.secondary)
}
Spacer() // ๋จ์ ๊ณต๊ฐ์ ๋ฐ์ด๋
Image(systemName: "chevron.right")
.foregroundStyle(.tertiary)
}
.padding()
.background(Color(.secondarySystemBackground))
.clipShape(RoundedRectangle(cornerRadius: 12))
// 2. ZStack: ์๋ค ๋ ์ด์ด ์ค์ฒฉ (๋ฐฐ๊ฒฝ, ์ค๋ฒ๋ ์ด, ๋ฑ์ง)
ZStack(alignment: .topTrailing) {
RoundedRectangle(cornerRadius: 16)
.fill(LinearGradient(
colors: [.indigo, .purple],
startPoint: .topLeading,
endPoint: .bottomTrailing
))
.frame(height: 120)
// ์ฐ์ธก ์๋จ ๋ฑ์ง ์ค๋ฒ๋ ์ด
Text("PRO")
.font(.caption2)
.fontWeight(.heavy)
.padding(.horizontal, 8)
.padding(.vertical, 4)
.background(.ultraThinMaterial)
.foregroundStyle(.white)
.clipShape(Capsule())
.padding(12)
}
// 3. LazyVGrid: 2์ด ๊ฒฉ์ ๊ทธ๋ฆฌ๋ ๋ ์ด์์
Text("์๋น์ค ์นดํ
๊ณ ๋ฆฌ")
.font(.headline)
LazyVGrid(columns: columns, spacing: 12) {
ForEach(1...4, id: \.self) { index in
VStack(spacing: 8) {
Image(systemName: "cube.fill")
.font(.title2)
.foregroundStyle(.blue)
Text("๋ชจ๋ \(index)")
.font(.subheadline)
.fontWeight(.medium)
}
.frame(maxWidth: .infinity)
.padding(.vertical, 20)
.background(Color(.tertiarySystemBackground))
.clipShape(RoundedRectangle(cornerRadius: 12))
}
}
}
.padding()
}
}
}
4.2 ๋ทฐ ์์ ์(View Modifier) ์ฒด์ด๋ ๊ท์น
SwiftUI์์ ์์ ์๋ ํธ์ถ ์์๋๋ก ๋ทฐ๋ฅผ ์๋ก์ด ๋ทฐ๋ก ๋ํํ๋ฏ๋ก ์ฒด์ด๋ ์์๊ฐ ๊ฒฐ๊ณผ์ ์ ๋์ ์ธ ์ํฅ์ ๋ฏธ์นฉ๋๋ค.
// โ ๏ธ [์ฃผ์] ์์ ์ ์์์ ๋ฐ๋ฅธ ์์ ํ ๋ค๋ฅธ ๋ ๋๋ง ๊ฒฐ๊ณผ
struct ModifierOrderDemo: View {
var body: some View {
HStack(spacing: 24) {
// ์ฌ๋ฐ๋ฅธ ์์: ์์ชฝ ํจ๋ฉ -> ๋ฐฐ๊ฒฝ์ -> ์ธ๊ณฝ์ ํด๋ฆฌํ
Text("ํจ๋ฉ ํ ๋ฐฐ๊ฒฝ")
.padding(16)
.background(Color.green)
.clipShape(RoundedRectangle(cornerRadius: 10))
.foregroundStyle(.white)
// ์๋ชป๋ ์์: ๋ฐฐ๊ฒฝ์์ด ํ
์คํธ์๋ง ๋ถ๊ณ ๋ฐ๊นฅ์ผ๋ก ํจ๋ฉ์ด ์ ์ฉ๋จ
Text("๋ฐฐ๊ฒฝ ํ ํจ๋ฉ")
.background(Color.green) // ๊ธ์ ์์ญ๋ง ์ด๋ก์
.padding(16) // ์ด๋ก์ ์์ญ ๋ฐ๊นฅ์ ์ฌ๋ฐฑ ์์ฑ
.clipShape(RoundedRectangle(cornerRadius: 10))
.foregroundStyle(.white)
}
}
}
4.3 ๋์ ๋ชฉ๋ก(List) & ๋ชจ๋ ๋ผ์ฐํ (NavigationStack)
iOS 16+๋ถํฐ ๋์ ๋ ํ์ ์์ (Type-safe) ๊ฒฝ๋ก ๊ธฐ๋ฐ NavigationStack์ ๋ํ ์ฑ์ ๋ฅ๋งํฌ ๋ฐ ํ๋ฉด ์ด๋์ ํ์ค์ ๋๋ค.
import SwiftUI
// ์ด๋ํ ํ๋ฉด์ ๋์ ๋ฐ์ดํฐ ๋ชจ๋ธ (Hashable ํ์ ๊ตฌํ)
struct TechTopic: Identifiable, Hashable {
let id = UUID()
let name: String
let category: String
let detailDescription: String
}
struct TopicListView: View {
@State private var topics: [TechTopic] = [
TechTopic(name: "Swift 6 Concurrency", category: "Language", detailDescription: "๋ฐ์ดํฐ ๋ ์ด์ค ์์ ์ฑ์ ์ปดํ์ผ ํ์์ ๋ณด์ฅํ๋ ๋ชจ๋ ๋์์ฑ ๋ชจ๋ธ"),
TechTopic(name: "SwiftUI Observation", category: "UI", detailDescription: "@Observable ๋งคํฌ๋ก๋ฅผ ํตํ ์ธ๋ฐํ ํ๋กํผํฐ ๋จ์ ๋ทฐ ๋ฌดํจํ"),
TechTopic(name: "Swift Data", category: "Persistence", detailDescription: "Core Data๋ฅผ ๋์ฒดํ๋ ์์ Swift ๊ธฐ๋ฐ ๊ฐ์ฒด ์์์ฑ ํ๋ ์์ํฌ")
]
// ๋ค๋น๊ฒ์ด์
๊ฒฝ๋ก ์คํ ์ํ
@State private var navigationPath = NavigationPath()
var body: some View {
NavigationStack(path: $navigationPath) {
List {
ForEach(topics) { topic in
// ๋ฐ์ดํฐ๋ฅผ ๊ฐ(Value)์ผ๋ก ์ ๋ฌ
NavigationLink(value: topic) {
VStack(alignment: .leading, spacing: 4) {
Text(topic.name)
.font(.headline)
Text(topic.category)
.font(.caption)
.foregroundStyle(.secondary)
}
.padding(.vertical, 4)
}
.swipeActions(edge: .trailing, allowsFullSwipe: true) {
Button(role: .destructive) {
topics.removeAll { $0.id == topic.id }
} label: {
Label("์ญ์ ", systemImage: "trash")
}
}
}
}
.navigationTitle("ํ์ต ์ฃผ์ ๋ชฉ๋ก")
// NavigationLink(value:)์ ๋ชฉ์ ์ง ๋ฐ์ธ๋ฉ
.navigationDestination(for: TechTopic.self) { selectedTopic in
TopicDetailView(topic: selectedTopic)
}
// ๋น๊ฒจ์ ์๋ก๊ณ ์นจ (Pull-to-Refresh)
.refreshable {
try? await Task.sleep(nanoseconds: 1_000_000_000)
print("๋ฐ์ดํฐ ๊ฐฑ์ ์๋ฃ")
}
}
}
}
// ์์ธ ํ๋ฉด
struct TopicDetailView: View {
let topic: TechTopic
@Environment(\.dismiss) private var dismiss // ํ๋ฉด ๋ซ๊ธฐ ํ๊ฒฝ ๋ณ์
var body: some View {
VStack(alignment: .leading, spacing: 16) {
Text(topic.category)
.font(.caption)
.fontWeight(.bold)
.padding(.horizontal, 10)
.padding(.vertical, 4)
.background(Color.blue.opacity(0.1))
.foregroundStyle(.blue)
.clipShape(Capsule())
Text(topic.name)
.font(.largeTitle)
.fontWeight(.bold)
Text(topic.detailDescription)
.font(.body)
.foregroundStyle(.secondary)
.lineSpacing(6)
Spacer()
Button("๋ค๋ก ๊ฐ๊ธฐ") {
dismiss()
}
.frame(maxWidth: .infinity)
.padding()
.background(Color(.secondarySystemBackground))
.clipShape(RoundedRectangle(cornerRadius: 12))
}
.padding(20)
.navigationBarTitleDisplayMode(.inline)
}
}
4.4 iOS 17+ ์ ๊ท @Observable ์ํ ๊ด๋ฆฌ ์์ ์ ๋ณต
๊ธฐ์กด ObservableObject์ @Published๋ ์์ฑ ์ค ํ๋๋ง ๋ณ๊ฒฝ๋์ด๋ ๋ทฐ ์ ์ฒด๊ฐ ๋ค์ ํ๊ฐ๋์์ต๋๋ค.
iOS 17์ @Observable ๋งคํฌ๋ก๋ ์ค์ ๋ทฐ์์ ์ฝ์(Read) ํน์ ์์ฑ๋ง์ ์ถ์ ํ์ฌ ์ต์ํ์ ๋ฒ์๋ง ๋ฆฌ๋ ๋๋งํฉ๋๋ค.
import SwiftUI
import Observation
// MARK: - [1] ๋น์ฆ๋์ค ์ํ ๋ชจ๋ธ ์ ์ (@Observable ๋งคํฌ๋ก)
@Observable
final class UserProfileModel {
var username: String = "LuckyDeveloper"
var isSubscribed: Bool = false
var notificationCount: Int = 5
func upgradeSubscription() {
isSubscribed = true
}
}
// MARK: - [2] ๋ถ๋ชจ ๋ทฐ (@State๋ก ์ธ์คํด์ค ์๋ช
์ฃผ๊ธฐ ๊ด๋ฆฌ)
struct UserProfileContainerView: View {
@State private var userModel = UserProfileModel()
var body: some View {
VStack(spacing: 20) {
Text("์ฌ์ฉ์ ๊ณ์ ์ผํฐ")
.font(.title3)
.fontWeight(.bold)
// ์์ ์ปดํฌ๋ํธ์ ๋ชจ๋ธ ์ง์ ์ฐธ์กฐ ์ ๋ฌ
ProfileStatusCard(model: userModel)
// ์๋ฐฉํฅ ๋ฐ์ธ๋ฉ ์ ๋ฌ (@Bindable ์ฌ์ฉ)
ProfileEditForm(model: userModel)
}
.padding()
}
}
// MARK: - [3] ์ฝ๊ธฐ ์ ์ฉ ์์ ๋ทฐ (ํน์ ์์ฑ๋ง ๊ด์ฐฐ)
struct ProfileStatusCard: View {
let model: UserProfileModel
var body: some View {
// notificationCount๊ฐ ๋ณ๊ฒฝ๋์ด๋ isSubscribed๋ฅผ ์ฝ์ง ์๋ ๋ค๋ฅธ ๋ทฐ๋ ์ฌ๋ ๋๋ง๋์ง ์์
HStack {
VStack(alignment: .leading, spacing: 4) {
Text(model.username)
.font(.headline)
Text(model.isSubscribed ? "VIP ํ์" : "์ผ๋ฐ ํ์")
.font(.caption)
.foregroundStyle(model.isSubscribed ? .green : .secondary)
}
Spacer()
if !model.isSubscribed {
Button("๊ตฌ๋
ํ๊ธฐ") {
model.upgradeSubscription()
}
.buttonStyle(.borderedProminent)
}
}
.padding()
.background(Color(.secondarySystemBackground))
.clipShape(RoundedRectangle(cornerRadius: 12))
}
}
// MARK: - [4] ์๋ฐฉํฅ ๋ฐ์ธ๋ฉ ์์ ๋ทฐ (@Bindable ํ์ฉ)
struct ProfileEditForm: View {
// @Observable ํด๋์ค์ ์์ฑ์ TextField ๋ฑ์ $๋ฐ์ธ๋ฉ์ผ๋ก ๋ณํํ ๋ ์ฌ์ฉ
@Bindable var model: UserProfileModel
var body: some View {
VStack(alignment: .leading, spacing: 8) {
Text("์ด๋ฆ ๋ณ๊ฒฝ")
.font(.caption)
.foregroundStyle(.secondary)
TextField("์ฌ์ฉ์๋ช
์ ์
๋ ฅํ์ธ์", text: $model.username)
.textFieldStyle(.roundedBorder)
Toggle("ํ๋ฆฌ๋ฏธ์ ๊ตฌ๋
ํ์ฑํ", isOn: $model.isSubscribed)
.padding(.top, 8)
}
.padding()
.background(Color(.tertiarySystemBackground))
.clipShape(RoundedRectangle(cornerRadius: 12))
}
}
4.5 ๋ชจ๋ฌ, ์ํธ(Sheet) ๋ฐ ์๋ฆผ์ฐฝ(Alert)
import SwiftUI
struct SheetAlertDemoView: View {
@State private var isShowingSheet: Bool = false
@State private var isShowingAlert: Bool = false
@State private var selectedItemTitle: String = ""
var body: some View {
VStack(spacing: 16) {
// 1. ํ๋จ ๋ฐํ
์ํธ (.sheet)
Button("์ค์ ๋ฐํ
์ํธ ์ด๊ธฐ") {
isShowingSheet = true
}
.buttonStyle(.borderedProminent)
.sheet(isPresented: $isShowingSheet) {
SettingsSheetView()
.presentationDetents([.medium, .large]) // iOS 16+ ๋์ด ์กฐ์
.presentationDragIndicator(.visible)
}
// 2. ์์คํ
ํ์
์๋ฆผ (.alert)
Button("๋ฐ์ดํฐ ์ด๊ธฐํ ๊ฒฝ๊ณ ") {
selectedItemTitle = "์บ์ ๋ฐ์ดํฐ"
isShowingAlert = true
}
.buttonStyle(.bordered)
.tint(.red)
.alert("์ ๋ง ์ญ์ ํ์๊ฒ ์ต๋๊น?", isPresented: $isShowingAlert) {
Button("์ทจ์", role: .cancel) { }
Button("์ญ์ ", role: .destructive) {
print("\(selectedItemTitle)๊ฐ ์ญ์ ๋์์ต๋๋ค.")
}
} message: {
Text("\(selectedItemTitle) ์ญ์ ์ ๋ณต๊ตฌํ ์ ์์ต๋๋ค.")
}
}
}
}
struct SettingsSheetView: View {
@Environment(\.dismiss) private var dismiss
var body: some View {
NavigationStack {
List {
Section("์๋ฆผ ์ค์ ") {
Toggle("ํธ์ ์๋ฆผ ์์ ", isOn: .constant(true))
}
Section("๊ณ์ ") {
Text("๋ฒ์ 1.0.0 (Build 2026)")
}
}
.navigationTitle("ํ๊ฒฝ ์ค์ ")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .topBarTrailing) {
Button("์๋ฃ") {
dismiss()
}
}
}
}
}
}
5. [3๋จ๊ณ] ์ค๋ฌด ์์ฉ ํจํด (MVVM & Async Networking)
์ค์ ํ๋ก๋์
์ฑ์์ ์ฌ์ฉํ๋ REST API ํต์ , Swift Concurrency(async/await), ViewModel ์ํคํ
์ฒ๋ฅผ ์์ ํ ๊ตฌํํ ์ฝ๋์
๋๋ค.
5.1 ๋ฐ์ดํฐ ๋ชจ๋ธ ๋ฐ API ๋คํธ์ํฌ ์๋น์ค ๊ณ์ธต
import Foundation
// MARK: - [Model] Codable & Identifiable ๋ฐ์ดํฐ ๋ชจ๋ธ
struct PostItem: Identifiable, Codable, Hashable {
let id: Int
let title: String
let body: String
}
// MARK: - [Network Service] ํต์ ํ๋กํ ์ฝ ๋ฐ ๋น๋๊ธฐ ํด๋ผ์ด์ธํธ
protocol PostServiceProtocol: Sendable {
func fetchPosts() async throws -> [PostItem]
}
final class PostNetworkService: PostServiceProtocol {
func fetchPosts() async throws -> [PostItem] {
guard let url = URL(string: "https://jsonplaceholder.typicode.com/posts?_limit=10") else {
throw URLError(.badURL)
}
let (data, response) = try await URLSession.shared.data(from: url)
guard let httpResponse = response as? HTTPURLResponse, (200...299).contains(httpResponse.statusCode) else {
throw URLError(.badServerResponse)
}
let decoder = JSONDecoder()
return try decoder.decode([PostItem].self, from: data)
}
}
5.2 @Observable ViewModel (@MainActor ์ค๋ ๋ ์์ ์ฑ ๋ณด์ฅ)
import SwiftUI
import Observation
// UI ๋ณ๊ฒฝ์ ๋ฐ๋์ ๋ฉ์ธ ์ค๋ ๋์์ ์ผ์ด๋จ์ ์ปดํ์ผ๋ฌ ์์ค์์ ๋ณด์ฅ (@MainActor)
@Observable
@MainActor
final class PostListViewModel {
// UI ๊ด์ฐฐ ์ํ
var posts: [PostItem] = []
var isLoading: Bool = false
var errorMessage: String? = nil
private let service: PostServiceProtocol
init(service: PostServiceProtocol = PostNetworkService()) {
self.service = service
}
func loadPosts() async {
guard !isLoading else { return }
isLoading = true
errorMessage = nil
do {
self.posts = try await service.fetchPosts()
} catch {
self.errorMessage = "๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์ค๋ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: \(error.localizedDescription)"
}
isLoading = false
}
}
5.3 MVVM ๋ทฐ ํตํฉ (.task ์๋ช
์ฃผ๊ธฐ ์ฐ๋)
import SwiftUI
struct PostListView: View {
@State private var viewModel = PostListViewModel()
var body: some View {
NavigationStack {
ZStack {
if viewModel.isLoading && viewModel.posts.isEmpty {
// ๋ก๋ฉ ์ธ๋์ผ์ดํฐ
ProgressView("๋คํธ์ํฌ ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์ค๋ ์ค...")
.scaleEffect(1.2)
} else if let error = viewModel.errorMessage {
// ์๋ฌ ๋ฐ์ ์ ์ฌ์๋ UI
VStack(spacing: 16) {
Image(systemName: "exclamationmark.triangle.fill")
.font(.system(size: 44))
.foregroundStyle(.orange)
Text(error)
.font(.subheadline)
.multilineTextAlignment(.center)
.foregroundStyle(.secondary)
.padding(.horizontal)
Button("๋ค์ ์๋") {
Task {
await viewModel.loadPosts()
}
}
.buttonStyle(.borderedProminent)
}
} else {
// ์ฑ๊ณต ๋ชฉ๋ก
List(viewModel.posts) { post in
VStack(alignment: .leading, spacing: 8) {
Text(post.title)
.font(.headline)
.lineLimit(1)
Text(post.body)
.font(.caption)
.foregroundStyle(.secondary)
.lineLimit(2)
}
.padding(.vertical, 4)
}
.refreshable {
await viewModel.loadPosts()
}
}
}
.navigationTitle("์ต์ ํผ๋ (REST API)")
// ๋ทฐ๊ฐ ํ๋ฉด์ ๋ํ๋ ๋ ๋น๋๊ธฐ ์์
์์ ๋ฐ ๋ทฐ ์๋ฉธ ์ ์๋ ์ทจ์
.task {
await viewModel.loadPosts()
}
}
}
}
6. ์ ๋๋ฉ์ด์ & ์ ์ค์ฒ (Interactive UI)
SwiftUI๋ ์ํ ๋ณ๊ฒฝ์ ์ ๋๋ฉ์ด์ ๋ธ๋ก์ผ๋ก ๊ฐ์ธ๊ธฐ๋ง ํ๋ฉด ์คํ๋ง ๋ฌผ๋ฆฌ ๊ธฐ๋ฐ ์ธํฐ๋์ ์ ์ฆ์ ๋ ๋๋งํฉ๋๋ค.
import SwiftUI
struct InteractiveAnimationDemo: View {
@State private var isExpanded: Bool = false
@State private var dragOffset: CGSize = .zero
var body: some View {
VStack(spacing: 32) {
// 1. ๋๋๊ทธ ์ ์ค์ฒ ๋ฐ ์์น ๋ฆฌ์
์ ๋๋ฉ์ด์
RoundedRectangle(cornerRadius: 20)
.fill(Color.blue.gradient)
.frame(width: 140, height: 140)
.overlay(
Text("๋๋๊ทธ ํด๋ณด์ธ์")
.font(.caption)
.fontWeight(.bold)
.foregroundStyle(.white)
)
.offset(dragOffset)
.gesture(
DragGesture()
.onChanged { value in
dragOffset = value.translation
}
.onEnded { _ in
// ์์ ๋์ ๋ ํ์ฑ ์๊ฒ ์ ์๋ฆฌ ๋ณต๊ท
withAnimation(.spring(response: 0.4, dampingFraction: 0.5)) {
dragOffset = .zero
}
}
)
// 2. ๋์ด ํ์ฅ ๋ฐ ํ์ ์ ๋๋ฉ์ด์
VStack {
HStack {
Text("์์ธ ์ ๋ณด ์์ฝ๋์ธ")
.font(.headline)
Spacer()
Image(systemName: "chevron.down")
.rotationEffect(.degrees(isExpanded ? 180 : 0))
}
.contentShape(Rectangle())
.onTapGesture {
withAnimation(.easeInOut(duration: 0.3)) {
isExpanded.toggle()
}
}
if isExpanded {
Text("SwiftUI์ ์ ์ธํ ์ ๋๋ฉ์ด์
์ ๋ ๋๋ง ํ์ดํ๋ผ์ธ์์ ๋ณด๊ฐ(Interpolation)์ ํ๋์จ์ด ๊ฐ์์ผ๋ก ์ฐ์ฐํฉ๋๋ค.")
.font(.subheadline)
.foregroundStyle(.secondary)
.padding(.top, 8)
.transition(.opacity.combined(with: .move(edge: .top)))
}
}
.padding(16)
.background(Color(.secondarySystemBackground))
.clipShape(RoundedRectangle(cornerRadius: 14))
}
.padding()
}
}
7. ์ค๋ฌด ์ฃผ์์ฌํญ & ํ๋ก๋์ ์ฒดํฌ๋ฆฌ์คํธ
โ ๏ธ ์์ฃผ ๋ฐ์ํ๋ ์ค์ & ์ํฐํจํด ๋ฐฉ์ง
-
View๊ฐ ์ ํด๋์ค๊ฐ ์๋struct์ธ๊ฐ์?- SwiftUI ๋ทฐ๋ UIKit์ฒ๋ผ ํ๋ฉด์ ์ฅ๊ธฐ ์ฒด๋ฅํ๋ ๋ฌด๊ฑฐ์ด ์ธ์คํด์ค๊ฐ ์๋๋ผ **โ๊ฐ๋ณ๊ณ ์ผ์์ ์ธ ๋ ๋๋ง ๋ช ์ธ์โ**์ ๋๋ค.
- ์ํ๊ฐ ๋ณ๊ฒฝ๋๋ฉด ์์ญ ๋ฒ ์ฌ์์ฑ๋๋ฏ๋ก, ํ(Heap) ํ ๋น ๋น์ฉ์ด ์๋ **์คํ(Stack) ๊ธฐ๋ฐ ๊ฐ ํ์
struct**๋ก ์ค๊ณ๋์ด ์์ต๋๋ค.
-
body๋ด๋ถ์์ ๋ฌด๊ฑฐ์ด ๋น์ฆ๋์ค ๋ก์ง ์ฐ์ฐ ์ ๋ ๊ธ์งbody์์ฑ์ ์ํ ๋ณ๊ฒฝ ์ ์ด๋น 60~120ํ ์ฌํ๊ฐ๋ ์ ์์ต๋๋ค.- ๋ ์ง ํฌ๋งคํ
(
DateFormatter()), ์ ๋ ฌ(sorted()), ๋์ฉ๋ ํํฐ๋ง ์ฐ์ฐ์ ViewModel์ด๋ ์ฌ์ ์ ์บ์ฑ๋ ํ๋กํผํฐ๋ก ์์ํ์ธ์.
-
AnyView๋จ์ฉ ๊ธ์ง- ๋ฐํ ํ์
์ ํต์ผํ๊ธฐ ์ํด
AnyView๋ก ๋ฐ์ฑํ๋ฉด SwiftUI์ ๋ด๋ถ ํ์ ํธ๋ฆฌ Diffing ์๊ณ ๋ฆฌ์ฆ์ด ๊นจ์ ธ ๋ทฐ ์ ์ฒด๊ฐ ๊ฐ์ ์ฌ์์ฑ๋ฉ๋๋ค. - ์กฐ๊ฑด๋ถ ๋ถ๊ธฐ์๋
@ViewBuilder์ด๋ ธํ ์ด์ ์ด๋Group์ ์ฌ์ฉํ์ธ์.
- ๋ฐํ ํ์
์ ํต์ผํ๊ธฐ ์ํด
-
๋น๋๊ธฐ ๋ฉ๋ชจ๋ฆฌ ๋์ ๋ฐฉ์ง
onAppear์์ ๋น๋๊ธฐTask.init์ ์๋ ์์ฑํ์ง ๋ง์ธ์. ํ๋ฉด์ ๋ฒ์ด๋๋ ์ทจ์๋์ง ์์ ๋ฉ๋ชจ๋ฆฌ ๋์๊ฐ ๋ฐ์ํฉ๋๋ค.- ๋ทฐ ์๋ช
์ฃผ๊ธฐ์ ์๋์ผ๋ก ๋ฐ์ธ๋ฉ๋์ด ํ๋ฉด ์ดํ ์ ์์
์ ์๋ ์ทจ์ํ๋
.task { }์์ ์๋ฅผ ์ฌ์ฉํ์ธ์.
๐ App Store ๋ฐฐํฌ ํ๋ก๋์ ์ฒดํฌ๋ฆฌ์คํธ
- Privacy Manifests (
PrivacyInfo.xcprivacy): iOS 17+ ํ์. ์ถ์ ๋๋ฉ์ธ ๋ฐ ํ์ ์ ์ธ API(NSPrivacyAccessedAPITypes: ๋์คํฌ ์ฉ๋, ํ์์คํฌํ ๋ฑ)๋ฅผ ๋๋ฝ ์์ด plist์ ๊ธฐ์ฌํ๋์ง ์ ๊ฒ. - ๋คํฌ ๋ชจ๋ & ๋ค์ด๋ด๋ฏน ํ์
(Dynamic Type) ๋์: ํ๋์ฝ๋ฉ๋ ์์(
Color.black) ๋์Color(.label),.foregroundStyle(.primary)๋ฑ ์๋งจํฑ ์์คํ ์ปฌ๋ฌ ์ฌ์ฉ ํ์ธ. - ์ธ์ฆ์ & ํ๋ก๋น์ ๋ ํ๋กํ์ผ: App Store Connect ๊ณ์ ๊ณผ Xcode Signing & Capabilities ์๋ ์๋ช ์ฐ๋.
- App Icon & ๋ฐ์น ์คํฌ๋ฆฐ: 1024x1024 ์ฑ๊ธ ์ฌ์ด์ฆ ๋ฒกํฐ Asset ์ ์ฉ ์ฌ๋ถ ์ ๊ฒ.
- Swift 6 ๋ชจ๋ ๋์์ฑ ๊ฒฝ๊ณ ์ ๊ฒ: Xcode Build Settings์
Strict Concurrency Checking์Complete๋ก ์ค์ ํ์ฌ ๋ฐ์ดํฐ ๋ ์ด์ค ์ ์ฌ ๋ฒ๊ทธ ์ฌ์ ์ฐจ๋จ.
์๊ฒฌ ๋ฐ ์ง๋ฌธ
0์์ง ๋ฑ๋ก๋ ์๊ฒฌ์ด ์์ต๋๋ค. ์ฒซ ๋ฒ์งธ ๋๊ธ์ ๋จ๊ฒจ๋ณด์ธ์!
๋๊ธ ์์
๋๊ธ ์ญ์