์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
- SwiftUI
- TCA
- SnapKit
- visionOS
- Swfit
- Lv2
- node.js
- CollectionView
- reactorkit
- raywenderlich
- tableView
- BOJ
- UIKit
- arkit
- designpattern
- combine
- ํ๋ก๊ทธ๋๋จธ์ค
- Flutter
- MVVM
- BFS
- rxcocoa
- Xcode
- Kuring
- swift
- RxSwift
- ๋ฐฑ์ค
- ios
- realm
- XCTest
- ํจ์คํธ์บ ํผ์ค
- Today
- Total
lgvv98
ch10 ํ์๊ธ ๋ญํน์ฑ ์ฝ๋๋ฆฌ๋ทฐ(MVVM) ๋ณธ๋ฌธ
ch10 ํ์๊ธ ๋ญํน์ฑ ์ฝ๋๋ฆฌ๋ทฐ(MVVM)
๐ฅ ์บ๋ฟ๋งจ 2021. 6. 21. 22:37์ด๋ฒ ์๊ฐ์๋ ๊ฐ์๋ ์งง์์ง๋ง, ๊ณต๋ถํ ๋ด์ฉ์ ์์ฐฝ์ธ ์๊ฐ์ด์๋ค...
(๋ชฉ์ฐจ)
- MVVM Review
- Model ์ค์ ์ค๋ช
- ViewModel ์ค์ ์ค๋ช
- View ์ค์ ์ค๋ช
- ๊ตฌ์กฐ์ฒด ๋ฐฐ์ด์ ์ ๋ ฌํ๋ ์ฝ๋์ ๋ํด์ ์ ๋ฆฌ
โ MVVM Review
- Model : ๋ฐ์ดํฐ(struct)
- View : UI์์(UIView, UIViewController)
- ViewModel : ์ค๊ณ์(ViewModel) -> ๋ทฐ์ ๋ชจ๋ธ์ด ์ง์ ์ํตํ๋๊ฒ ์๋๋ผ, ViewModel์ ํตํด ๊ฐ์ ์ํตํด์ผ ํ๋ค.
๐ก์ด๋ก ์ ๋ํด ๋ ๊ตฌ์ฒด์ ์ธ ์ฌํญ์ด ๊ถ๊ธํ๋ค๋ฉด...
2021.06.21 - [iOS/ํจ์บ ๊ณต๋ถ] - MVVM ํจํด
MVVM ํจํด
๋์์ธ ํจํด ๊ธฐ์ด #1 ํด๋ฆฐ ์ํคํ ์ณ ๐ ๋์์ธ ํจํด ๐ ์ ๋ํ ๊ณ ๋ฏผ!! Goal 1. ๊ธฐ์ ๋ถ์ฑ ์ต์ํ ๐ธ 2. ์ฌ์ฌ์ฉ ๋ฐ ์ง์ ๊ฐ๋ฅ โป๏ธ ๋์์ธ ํจํด ๊ธฐ์ด #2 MVC(Model - View - Controller) Model : ๋ฐ์ดํฐ(Struct) V..
rldd.tistory.com
โ ์ ๊ทธ๋ผ ์ด์ ๋ณธ๊ฒฉ์ ์ผ๋ก ์ด๋ฒ ์๊ฐ์๋ ์ด๋ก ์ ๋ฐํ์ผ๋ก ์ฝ๋์์ MVVM ํจํด์ ์ ์ฉํด๋ณด๋ฉด์ ์ค์ ์์ ์ด๋ป๊ฒ ์ฌ์ฉ๋๊ณ ๋๋ง์ ์ธ์ด๋ฅผ ์ฌ์ฉํ์ฌ ์ ๋ฆฌํด๋ณด๋๋ก ํ์
1๏ธโฃ Model์ ๋ฐ์ดํฐ(struct)๋ผ๊ณ ํ์ง? ๋ง๊ทธ๋๋ struct๋ค.
struct BountyInfo {
let name : String
let bounty : Int
var image : UIImage? {
return UIImage(named: "\(name).jpg")
}
init(name : String, bounty: Int) {
self.name = name
self.bounty = bounty
}
}
๋ด ์ฝ๋์์ ์์ฑ๋ Model์ธ๋ฐ, struct๋ก BountyInfo๋ฅผ ๊ตฌ์ฑํ์๋ค.
Model์๋ ์ฐ๋ฆฌ๊ฐ ๊ฐ๊ณ ์๋ ๋ฐ์ดํฐ๊ณ , ๋ง์ฝ ์๋ฒ๊ฐ ์๋ค๋ฉด ์๋ฒ์์ ๊ฐ์ ธ์ฌ ๋ฐ์ดํฐ์ ๋ณ์๋ค์ ๋ชจ์๋ ์ค๋ธ์ ํธ ๊ฐ์ ๋๋์ด๋ค.
Model์๋ ์ ์ธ๊ณผ init๋ง ์ํํ๋ค.
์ฆ, ๋ฐ์ดํฐ๋ฅผ ์บก์ํ ํ๋ค๊ณ ์๊ฐํด!!
2๏ธโฃ ๋ค์์ ViewModel์ธ๋ฐ ์ฝ๋๋ฅผ ๋ณด๋ฉด์ ์ดํด๋ฅผ ๋์๋ณด์
class BountyViewModel {
let bountyInfoList : [BountyInfo] = [
BountyInfo(name: "brook", bounty: 33000000),
BountyInfo(name: "chopper", bounty: 50),
BountyInfo(name: "franky", bounty: 44000000),
BountyInfo(name: "luffy", bounty: 300000000),
BountyInfo(name: "nami", bounty: 16000000),
BountyInfo(name: "robin", bounty: 80000000),
BountyInfo(name: "sanji", bounty: 77000000),
BountyInfo(name: "zoro", bounty: 120000000)
]
var sortedList : [BountyInfo] {
let sortedList = bountyInfoList.sorted { prev, next in
return prev.bounty > next.bounty
}
return sortedList
}
var numOfBountyInfoList : Int {
return bountyInfoList.count
}
func bountyInfo(at index : Int) -> BountyInfo {
return sortedList[index]
}
}
ViewModel ์์๋ Model์ ์ฌ์ฉํด ๋ฐ์ดํฐ๋ฅผ ๊ฐ๊ณตํ๋ ์์ ์ ์ฌ๊ธฐ์ ์ํํ๋ค.
์กฐ๊ธ ํน์ดํ ์ ์ ๋ณ์๋ฅผ ์ ์ธํ ๋ return์ ์ฌ์ฉํ๋ค๋ ๊ฒ์ธ๋ฐ, ๋ค๋ฅธ ๊ณณ์์ ์ฐธ์กฐํ ๋, ๋ฐ์ดํฐ ๊ฐ์ return ํด์ค์ผ ํ๊ธฐ ๋๋ฌธ!
3๏ธโฃ ๋ง์ง๋ง์ View
๋ทฐ๋ ์ฝ๋๋ฅผ ๋ฐ๋ก ์ฒจ๊ฐํ์ฌ ์ค๋ช ํ์ง๋ ์๊ฒ ๋๋ฐ, View์๋ UI๊ตฌ์ฑ๊ณผ ๊ด๋ จํ ์ฝ๋๋ ํ์์ ์์ฑํ๋๋๋ก ์์ฑํ๋ฉด ๋ผ.
โ๏ธ์ฃผ์ํ ์ ์ ๋ณ์๋ฅผ Model์์ ์ ์ธํด์ผ ํ๋ค๋ ๊ฒ๊ณผ ์ฐธ์กฐํ ๋์๋ ViewModel์ ํตํด์ ๋ฐ์ดํฐ๋ฅผ ์ฐธ์กฐํด์ผํ๋ค๋ ์ ์ด์ผ.
4๏ธโฃ ๊ตฌ์กฐ์ฒด ๋ฐฐ์ด์ ์ ๋ ฌํ๋ ์ฝ๋
let bountyInfoList : [BountyInfo] = [
BountyInfo(name: "brook", bounty: 33000000),
BountyInfo(name: "chopper", bounty: 50),
BountyInfo(name: "franky", bounty: 44000000),
BountyInfo(name: "luffy", bounty: 300000000),
BountyInfo(name: "nami", bounty: 16000000),
BountyInfo(name: "robin", bounty: 80000000),
BountyInfo(name: "sanji", bounty: 77000000),
BountyInfo(name: "zoro", bounty: 120000000)
] //C์์ ๊ตฌ์กฐ์ฒด ๋ฐฐ์ด๊ณผ ํ์์ด ๋น์ทํด์ ๊ตฌ์กฐ์ฒด ๋ฐฐ์ด์ด๋ผ๊ณ ์ด๋ฆ ๋ถ์
// ๊ตฌ์กฐ์ฒด ๋ฐฐ์ด์ ์ ์ธ
var sortedList : [BountyInfo] {
let sortedList = bountyInfoList.sorted { prev, next in
return prev.bounty > next.bounty
} // ๋ด๋ฆผ์ฐจ์์ผ๋ก ์ ๋ ฌ (ํฐ์์์ -> ์์์๋ก)
return sortedList // ๋ค๋ฅธ ๊ณณ์์ ์ฐธ์กฐํด์ผ ํ๊ธฐ ๋๋ฌธ์!
} // ๊ตฌ์กฐ์ฒด ๋ฐฐ์ด์ธ๋ฐ ์ ๋ ฌ๋ ๋ณ์
// ์ ์ธ๋ ๊ตฌ์กฐ์ฒด ๋ฐฐ์ด์ ๊ฐ๊ณ ์ ๋ ฌ
์ด๋ฐ ์ฝ๋๋ ์ฒ์๋ด์ ์๊ฐํ๋๋ฐ ๋์์ด ๋ง์ด ๋์๋ฐ. ์ฐ์ bounty๊ฐ์ ๊ฐ๊ณ ์ ๋ ฌํ ๊ฒ์ด ์๋นํ ์ ์ ํ๋ค!!
'โ ๏ธ deprecated โ ๏ธ > ํจ์บ (์ฌ์ธ์)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ch11 ํ์๊ธ ๋ญํน์ฑ ์ฝ๋๋ฆฌ๋ทฐ(CollectionView) (0) | 2021.06.22 |
---|---|
๐ ch11 Animation - ๊ฐ๋ ํธ๐ (0) | 2021.06.22 |
ch10 MVVM ํจํด (0) | 2021.06.21 |
ch09 ํ์๊ธ ๋ญํน์ฑ ์ฝ๋๋ฆฌ๋ทฐ (0) | 2021.06.21 |
ํจ์คํธ ์บ ํผ์ค iOS ๊ฐ์ (0) | 2021.06.20 |