์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
- rxcocoa
- ํ๋ก๊ทธ๋๋จธ์ค
- Flutter
- Swfit
- tableView
- TCA
- SnapKit
- visionOS
- Kuring
- ๋ฐฑ์ค
- reactorkit
- BFS
- SwiftUI
- Lv2
- BOJ
- ios
- designpattern
- ํจ์คํธ์บ ํผ์ค
- raywenderlich
- Xcode
- UIKit
- swift
- XCTest
- arkit
- realm
- CollectionView
- MVVM
- combine
- RxSwift
- node.js
- Today
- Total
lgvv98
[SwiftUI] EqutableView (feat. POD) ๋ณธ๋ฌธ
- EqutableView๋?
> EqutableView๋ ์ด์ ๊ฐ๊ณผ ๋น๊ตํ์ฌ ์ ๊ฐ์ด ์ด์ ๊ฐ๊ณผ ๋์ผํ ๊ฒฝ์ฐ ์์ ์
๋ฐ์ดํธ๋ฅผ ๋ฐฉ์งํ๋๋ก ์ค๊ณ๋ ๋ทฐ์
๋๋ค.
`iOS 13.0+` `iPadOS 13.0+` `tvOS 13.0` `watchOS 6.0+` `visionOS 1.0+ Beta
[apple-docs-EqutableView](https://developer.apple.com/documentation/swiftui/equatableview)
๐ก ๋ค์ด๊ฐ๊ธฐ ์ .
- ์๋์ ๊ฐ๋
์ ๋ํ ์ง์์ด ๋ฐ๋์ ํ์ํจ. ํ์ํ ์ด์ ๋ ๊ธ ์ ์ผ ์๋์์ ์ค๋ช
- [Plain ol' Data](https://rldd.tistory.com/551)
EqutableView Usage
1. ๋ด๊ฐ EqutableView๋ก ๋ง๋ค๊ณ ์ ํ๋ ๊ณณ์ Equtable์ ์ฑํ
struct LuckyBoxStateView: View, Equtable { ... }
2. ์ฌ์ฉํ๋ ๊ณณ์์
Equtable(content: LuckyBoxStateView(text: text))
or
/// โ
์ ๋ฐฉ์์ด ์ ์ธ์ ์ด๋ผ ๋ ๋์
LuckyBoxStateView(text: text)
.equtable()
์์ ์๋๋ฆฌ์ค
- ๋ด๊ฐ ๋ง๋๋ ์๋น์ค์ ํ๋ฅ ํ ์์ดํ
์ธ ๋ญํค๋ฐ์ค๊ฐ ์๋ค๊ณ ๊ฐ์ .
- ๋ญํค๋ฐ์ค๋ฅผ ๊ตฌ๋งค์ ํน์ ํ๋ฅ ์ ๋ฐ๋ผ ๋ฐฐ์๊ฐ ๊ฒฐ์ .
- ๋ญํค๋ฐ์ค ์คํ์ ๊ฒฐ์ ๋ ๋ฐฐ์๊ฐ UI์ ๋ํ๋๋ ๋ฐฉ์.
โ Code Smaple
import SwiftUI
enum LuckyBoxState: String {
case state1 = "x10๋ฐฐ"
case state2 = "x2๋ฐฐ"
case state3 = "x0.5๋ฐฐ"
case state4 = "x0.1๋ฐฐ"
static func result(number: Double) -> String {
if number < 5 {
return LuckyBoxState.state1.rawValue
} else if number < 15 {
return LuckyBoxState.state2.rawValue
} else if number < 45 {
return LuckyBoxState.state3.rawValue
} else {
return LuckyBoxState.state4.rawValue
}
}
}
struct ContentView: View {
@State private var number: Double = 0
@State private var count: UInt = 0
private let size: CGFloat = 200
var body: some View {
print("count: \(count) | number: \(number)")
return VStack {
LuckyBoxStateView(text: LuckyBoxState.result(number: number))
.equatable()
.frame(width: size, height: size * 2 / 3, alignment: .center)
.background(.blue.opacity(0.5))
.cornerRadius(10)
Button {
number = Double.random(in: 0..<100)
count += 1
} label: {
Text("๋ญํค๋ฐ์ค ์คํ")
}
Text("๋น์ฒจ๋ฒํธ: \(number)")
Text("์คํ์ฐจ์: \(count)")
}
}
}
struct LuckyBoxStateView: View, Equatable {
var text: String
static func == (lhs: LuckyBoxStateView, rhs: LuckyBoxStateView) -> Bool {
return lhs.text == rhs.text
}
var body: some View {
print("init")
return VStack(alignment: .center) {
Text("๐ ๋น์ฒจ ๐")
Spacer()
.frame(height: 20)
Text(text)
.font(.system(size: 40, weight: .semibold))
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
- ํด๋น ์ฝ๋๋ฅผ ์คํํ์ ๋์ ๊ฒฐ๊ณผ
๋ณด๋ค์ํผ ๊ฐ์ด ๋์ผํ ๊ฒฝ์ฐ init์ ์ํํ์ง ์์.
๐จ ์ฌ๊ธฐ์ ์ฃผ์ํด์ผ ํ๋ ์ ๐จ
์ด๊ฑฐ ์ง์ง ์ค์ํจ. ๊ฒฝ์ฐ์ ๋ฐ๋ผ์๋ EqutableView๋ก ๋ง๋ค์ด๋ ์๋ํ์ง ์๋ ๊ฒฝ์ฐ๋ ์กด์ฌ.
- ๋ง์ฝ LuckyBoxStateView์ member๊ฐ POD ํ์ ๋ง ์กด์ฌํ๋ ๊ฒฝ์ฐ ๊ฒฝ์ฐ์๋ EqutableView์์๋ ํด๋น `static func ==`์ด ํธ์ถ๋์ง ์๊ณ LuckyBoxStateView์ init์ด ํธ์ถ๋ฉ๋๋ค.
- non-POD๊ฐ ํ๋๋ผ๋ ์กด์ฌํ๋ ๊ฒฝ์ฐ ํด๋น ๋ถ๋ถ์ `static func ==`์ผ๋ก ๊ด๋ฆฌ๋๋ฉฐ @State์ @Biding์ ๊ฒฝ์ฐ์๋ ๋ด๋ถ์์ ์์ฒด์ ์ผ๋ก `static func ==`์ ๊ฒฐ์ ํ๊ณ ์์ต๋๋ค.
(์ฐธ๊ณ )
https://ios-development.tistory.com/1111
'apple > ๐ SwiftUI & Combine' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[SwiftUI] NavigationLink ํ์ดํ ์์ ๊ธฐ (0) | 2024.03.11 |
---|---|
[SwiftUI] keyboard ์ด๋ฒคํธ ํธ๋ค๋ง (0) | 2024.03.06 |
[iOS] NavigationSplitView (0) | 2023.08.05 |
[Combine] Networking(feat. RestAPI) (0) | 2022.06.11 |
[Combine] Let's study Combine! (feat. SwiftUI) (0) | 2022.06.03 |