PropertyWrapper
์ ์
- Swift 5.1 ๋ฒ์ ๋ถํฐ ๋์ ๋ ๋ฌธ๋ฒ
- ์ ์๋ property๊ฐ ์์ ๋ ํด๋น property๋ฅผ ๊ฐ์ธ์ ํน๋ณํ ํ์ ์ผ๋ก ๋ง๋ค์ด์ค๋ค
- ํ๋กํผํฐ๋ฅผ ์ ์ฅํ๋ ์ฝ๋์ ํ๋กํผํฐ๋ฅผ ์ ์ธํ๋ ์ฝ๋๋ฅผ ๊ด๋ฆฌํ๋ ๊ทธ ๊ฒฝ๊ณ๋ฅผ ๋๋ ์ฃผ๋ ๊ธฐ๋ฅ์ ์ถ๊ฐํด์ฃผ๋ ์ญํ
- ์ฌ์ฉ์๊ฐ ๋ณ๋์ ์ฝ๋ฉ ์์ด ์ด๋ ธํ ์ด์ ๋ง ์ ์ธํด๋ ๋ทฐ์์ ์์ ์ด๋ ์ฝ๊ธฐ๊ฐ ๊ฐ๋ฅํ๋๋ก ์บก์ํ๋ฅผ ๋์ ํด์ฃผ๋ ์ญํ
์ฌ์ฉ์ด์
- ํ๋กํผํฐ์ ์ ๊ทผ์ ํน์ ๋ก์ง์ ํตํด ์ ์ดํ ์ ์๊ฒ ํจ. —> ์ค๋ณต ์ฝ๋๋ฅผ ์ธ์คํด์ค๋ก ๋นผ๋ด๊ธฐ ๋๋ฌธ์ ์ค๋ณต์ ์ค์ผ ์ ์์.
- ํน์ ํ๋์ ์ ์ํ๋ ํ์ ์ ๋ง๋๋ ๊ฒ. ํนํ, ๊ฐ์ get-set์ ํ์ฉํ๋ ๋ฐ๋ณต๋๋ ๋ก์ง์ ์ฌ์ฌ์ฉ ํด์ผํ ๋ PropertyWrapper๋ก ์ ์ํ๊ณ ํด๋น ๋ก์ง ์๋ฆฌ์ ์ฌ์ฉํ๋ฉด ๋์ผํ ๋ก์ง์ ์ํํ๊ธฐ ๋๋ฌธ์ ์ค๋ณต์ฝ๋๋ฅผ ์ ๊ฑฐํ ์ ์์.
๋ง๋๋ ๋ฐฉ์: @propertyWrapper ์์ฑ์ ์ฌ์ฉํ์ฌ ๊ตฌ์กฐ์ฒด ๋ง๋ฌ
- ์ฐ์ฐ ํ๋กํผํฐ (get, set), ์ ์ฅ ํ๋กํผํฐ (didSet) ๋ฑ์ property wrapper๋ก ๋ง๋ค์ด ์ธ ์ ์์
- ํ๋กํผํฐ๋ฅผ ๊ฐ์ง๋ ํ์ ์์ ๋ถ์ฌ์ ์ฌ์ฉ (e.g. Class, Struct, Enum)
ProjectedValue
- ์ธ์คํด์ค ํ๋กํผํฐ๋ก ์ํ๊ฐ์ ๋ํ ๋ฐ์ธ๋ฉ (ํน์ ์ํ์ ๋ณํ๋ฅผ ๋ค๋ฃฐ๋ ์ฌ์ฉ)
- projectedValue๋ฅผ ์ฌ์ฉํ ๋๋ $๋ฅผ ๋ถ์ฌ ์ฌ์ฉ → @State ๊ฐ์ ๋ํผ ์ฌ์ฉ ์ $์ ๋ถ์ฌ์ฃผ๋ ๊ทผ๋ณธ์ ์ด์ !
- wrappedValue๋ wrapping๋ ๋ด๋ถ ๋ณ์๊ฐ์ ์ ๊ทผ, projectedValue๋ propertyWrapper ๋ด๋ถ์์ ์ ์ํ ๊ฐ์ ๋ฐํ —> ์ฆ, ํด๋น ํ๋กํผํฐ์ ์ถ๊ฐ๊ธฐ๋ฅ์ ์ ๊ทผํ ์ ์์.
References
Documentation
docs.swift.org
https://ios-development.tistory.com/895
[iOS - swift] @propertyWrapper์ projectedValue ๊ฐ๋ ($ ์ ๋์ฌ, ๋ฌ๋ฌ ์ ๋์ฌ)
PropertyWrapper ์ด๋ฏธ ์ ์๋ property๊ฐ ์์ ๋, ์ด property๋ฅผ ๊ฐ์ธ์(wrapping) computed-property๋ก ๋ง๋ ์๋ก์ด wrapper ํ๋กํผํฐ๋ฅผ ์๋ฏธ propertyWrapper ํ๋กํผํฐ ์์๋ wrappedValue๋ผ๋ property๊ฐ ์กด์ฌํ๋ฉฐ, ์ด proper
ios-development.tistory.com
@State
SwiftUI์์ ๊ฐ์ ์ฝ๊ณ ์ธ ์ ์๋ propertyWrapper (๊ฐ ์ ํ์ ์์ฑ์ ๋ํ ์ ์ฅ)
- ์ฆ, ๋ณํ๊ฐ ์๊ธฐ๋ฉด ํด๋น ๋ณ์์ ๊ฐ์ ์ฝ๊ฑฐ๋ ์๋ก ์ธ ์ ์์์ ์๋ฏธํจ.
- ๊ฐ์ ๋ณํ์ ๊ฐ์งํ๊ณ ํด๋น ๊ฐ์ ์์กดํ๋ ๋ทฐ๊ฐ ์๋์ผ๋ก ๋ค์ ๋ ๋๋ง ๋จ.
- ์ํ๋ฅผ ๋ณ๊ฒฝํ ๋๋ง๋ค body ์์ฑ์ด ์ฌ์ค์ ๋๊ธฐ ๋๋ฌธ. —> ํญ์ ์ต์ ๊ฐ ์ ์ง
์ธ์ ์ฌ์ฉํด์ผํ ๊น ?
- UI์ ์ํ๊ฐ๊ณผ ๊ฐ์ ํ์ ๋ ์ฉ๋๋ก๋ง ์ฌ์ฉํ๊ธฐ๋ฅผ ๊ถ๊ณ → ๋ทฐ ์์์๋ง ์ฌ์ฉํ๋ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ด๊ธฐ ๋๋ฌธ. ๋ทฐ ๋ฐ์ ํด๋์ค์์ ์ฌ์ฉํ๋ ค๋ฉด ObservableObject๋ฅผ ์ฌ์ฉํด์ผ ํจ.
์ private ์ ๊ทผ์ ์ด์๋ฅผ @State์ ํจ๊ป ์ฌ์ฉํ ๊น?
- @State ํ๋กํผํฐ๋ ํญ์ private์ผ๋ก ์ ์ธํ๊ณ ๊ฐ์ฅ ์์ ๋ทฐ์์ ๊ด๋ฆฌํด์ผ ํ๋ค.
- ํด๋น ๋ทฐ๊ฐ Data๋ฅผ ์์ ํ๊ณ ๊ด๋ฆฌํ๋ค๋ ๊ฐ๋ ์ ๋ช ์์ ์ผ๋ก ๋ํ๋ด๊ธฐ ์ํด์ ํจ๊ป ์ฌ์ฉํด์ผ ํ๋ค.
- ์ ์ธํ ๋ทฐ ์ธ์์ ๊ฐ์ ๋ณ๊ฒฝํ ์ ์๊ธฐ ๋๋ฌธ์ @State ๋ณ์๋ฅผ ํ์ฉํ๋ ค๋ฉด ๊ฐ์ฅ ์์๋ทฐ์ private ์ ๊ทผ ์ ์ด์๋ฅผ ๋ถ์ฌ์ ์ด๊ธฐํ ๋จ๊ณ์ ์ค์ ํ๋ ๊ฑธ ๋ฐฉ์งํ๋ค. (๋ทฐ ๋ด๋ถ ๋ฐ์์ ์ฌ์ฉ ๋ถ๊ฐ๋ฅํ๊ธฐ ๋๋ฌธ!)
- ๋ทฐ๋ฅผ ์ด๊ธฐํํ ๋, @State ํ๋กํผํฐ ๊ฐ๋ ๊ฐ์ด ์ด๊ธฐํํ๊ฒ๋๋ฉด SwiftUI์์ @State ํ๋กํผํฐ๋ฅผ ๊ด๋ฆฌํ๋ ๊ณต๊ฐ์ธ Storage์์ conflict๊ฐ ๋๊ธฐ ๋๋ฌธ
References
https://developer.apple.com/documentation/swiftui/state
State | Apple Developer Documentation
A property wrapper type that can read and write a value managed by SwiftUI.
developer.apple.com
https://velog.io/@suhwj/SWIFTUI-STATE
SWIFTUI @STATE
UIKit ์ ์ฌ์ฉํ์ ๋๋ ์ด๋ ํ ๋ณ์์ ๋ณํ๊ฐ ์๊ธฐ๋ฉด ํด๋น ๋ณํ๋ฅผ ์ง์ ๊ด์ฐฐํ๊ณ ๋ฐ์ ํด ์ฃผ์ด์ผ ํ๋๋ฐ SwiftUI ์์๋ property wrapper ๋ฅผ ํ์ฉํด ์ด๋ฌํ ์์ ์ ์๋ํ ํ ์ ์๋๋ก ํ๋ค.๋ณํ๊ฐ ์
velog.io
@Binding
๋ ๊ฐ์ ๋ทฐ ๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ๊ณต์ ํ๊ณ ์ ๋ฐ์ดํธ (๋ฐ์ดํฐ์ ์๋ฐฉํฅ ๋ฐ์ธ๋ฉ์ ๊ตฌํํ๊ธฐ ์ํด ์ฌ์ฉ๋๋ wrapper!)
์ธ์ ์ฌ์ฉํ ๊น?
- ๋ฐ์ธ๋ฉ ์์ฑ์ ๊ฐ์ ์ฐธ์กฐ๋ฅผ ์ ์ฅํ๊ธฐ ๋๋ฌธ์ ๋ถ๋ชจ ๋ทฐ์ ์์ ๋ทฐ๊ฐ ๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ์ฐธ์กฐํ๊ฒ ๋๋ค.
- ์์๋ทฐ → ์ ๋ฐ์ดํธ → ๋ถ๋ชจ๋ทฐ ๋ํ ์ ๋ฐ์ดํธ ๋์ด ๋์ผํ ๊ฐ ์ ์ง. ๋ฐ๋์ ๊ฒฝ์ฐ๋ ๊ฐ๋ค.
- ํ ๋ฐ์ธ๋ฉ ์์ฑ์ ์ฌ๋ฌ ๋ทฐ์์ ์ฌ์ฉํ๋ฉด, ํด๋น ๋ฐ์ดํฐ์ ๋ํ ๋ณ๊ฒฝ ์ฌํญ์ ๋ชจ๋ ์ฐ๊ฒฐ๋ ๋ทฐ์ ๋์์ ๋ฐ์.
Reference
https://developer.apple.com/documentation/swiftui/binding
Binding | Apple Developer Documentation
A property wrapper type that can read and write a value owned by a source of truth.
developer.apple.com
@StateObject
๊ด์ฐฐ์ค์ธ ๊ฐ์ฒด์ ๋ณ๊ฒฝ์ ๋ฐ์ํด์ ํ๋ฉด์ ์ ๋ฐ์ดํธํ ์ ์๊ฒ ํด์ฃผ๋ SwiftUI์ ํ๋กํผํฐ ๋ํผ
- @StateObject๋ฅผ ํตํด์ ๊ด์ฐฐ๋๊ณ ์๋ ๊ฐ์ฒด๋ ๊ทธ๋ค์ ๊ฐ์ง๊ณ ์๋ ํ๋ฉด ๊ตฌ์กฐ๊ฐ ๋ค์ ๋ ๋๋ง ๋์ด๋ ์ด๊ธฐํ ๋์ง ์๋๋ค.
- ๊ฐ์ฒด๊ฐ ๋ณํ๊ธฐ ์ ์ ํผ๋ธ๋ฆฌ์ฑํ๋ ํผ๋ธ๋ฆฌ์ ๋ฅผ ๊ฐ๋๋ค! ์ด ์ญํ ์ ํด๋์ค๋ฅผ ์ธ์คํด์คํํ๊ณ observableObject ๋ด์ ์๋ @Published ์์ฑ์ ๋ณ์๊ฐ ๋ณํํ ๋ willSet ๊ธฐ๋ฅ์ ํตํด ๋ทฐ๋ฅผ ์ ๋ฐ์ดํธ ํด์ค.
- @State vs @StateObject
- ์ฑ์ ํน์ ํ๋ฉด์์ ์ธ์คํด์คํ ํ๊ณ ์ ์งํ๋ คํ๋ observableObject ํด๋์ค์ ๋ฐ์ดํฐ๊ฐ ์กด์ฌํ ๋ ์ฌ์ฉํ๋ค. → Struct ํ์ ๋ง์ ์ฌ์ฉํ๋ ค๊ณ ํ๋ฉด @State๋ฅผ ์ฌ์ฉํ ๊ฒ.
References
https://developer.apple.com/documentation/swiftui/stateobject?changes=_9
StateObject | Apple Developer Documentation
A property wrapper type that instantiates an observable object.
developer.apple.com
https://pilgwon.github.io/post/state-object-vs-observed-object
[์์ํํธUI/๋ฒ์ญ] @StateObject์ @ObservedObject, ๋ฌด์์ด ๋ค๋ฅผ๊น์?
@StateObject vs. @ObservedObject: The differences explained
pilgwon.github.io
@ObservedObject
- ์ปด๋ฐ์ธ ์์ฑ
- ๊ด์ฐฐ๊ฐ๋ฅํ ๊ฐ์ฒด๋ฅผ ์์ฑํจ
- ObservableObject๋ฅผ ๊ตฌ๋ ํ๊ณ ํด๋น ๊ฐ์ฒด์ ๊ฐ์ด ์ ๋ฐ์ดํธ ๋ ๋๋ง๋ค ๋ทฐ๋ฅผ ๊ฐฑ์ ํ๋ PropertyWrapper.
- ์ฆ, ๋ชจ๋ธ์ด ๋ทฐ์ ๋ผ์ดํ ์ฌ์ดํด์ ์์กด์ ์ด๋ค.
References
https://developer.apple.com/documentation/swiftui/observedobject
ObservedObject | Apple Developer Documentation
A property wrapper type that subscribes to an observable object and invalidates a view whenever the observable object changes.
developer.apple.com
https://green1229.tistory.com/228
Combine - ObservableObject / @Published / @ObservedObject
์๋ ํ์ธ์. ๊ทธ๋ฆฐ์ ๋๋ค๐ข ์ด๋ฒ ํฌ์คํ ์์๋ ObservableObject๋ผ๋ ํ๋กํ ์ฝ์ ๋ํด ํ์ตํด๋ณด๊ฒ ์ต๋๋ค๐๐ป ์ถ๊ฐ๋ก ์ด์ด์ ธ์ @Published์ @ObservedObject๋ ๋ณด์์ฃ ! ์์ฆ ๊ฐ์ธ์ ์ผ๋ก ๋ง์ด ๋ฐ๋น ์ ธ์ ๋ธ
green1229.tistory.com
StateObeject vs. ObservedObject
- StateObeject์ ObservedObject๋ ๊ด์ฐฐ์ค์ธ ๊ฐ์ฒด์ ์ํ ๋ณ๊ฒฝ์ ๋ฐ์ํด์ ํ๋ฉด์ ์ ๋ฐ์ดํธํ ์ ์๊ฒ ํด์ฃผ๋ propertyWrapper์ด๋, ๋๋ ทํ ์ฐจ์ด๊ฐ ์กด์ฌํจ.
๊ณตํต์
- ObservableObject๋ฅผ ๊ตฌ๋
ํ์ฌ ์ด ๊ฐ์ฒด์ ๊ฐ ๋ณํ๋ฅผ ๊ฐ์งํ๋ฉด ๋ทฐ์ ๋ฐ์ํด์ค๋ค.
- ObservableObject๋โ ํด๋์ค ์ธ์คํด์ค๋ฅผ ๊ด์ฐฐํ๋ค๊ฐ ๊ฐ์ด ๋ณ๊ฒฝ๋๋ฉด ๋ทฐ๋ฅผ ์ ๋ฐ์ดํธ ํด์ค๋ค. → Class ํํ๋ก๋ง ์ฌ์ฉ์ด ๊ฐ๋ฅํ๋ค.
์ฐจ์ด์
- @ObservedObject ๊ฐ ๋ณํ๋ฅผ ๊ฐ์งํ๊ณ ๋ทฐ๊ฐ ๋ ๋๋ง ๋ ๋ ๋ทฐ๋ฅผ ์ฌ์์ฑ (๊ฐ์ ์ด๊ธฐํ ์ํจ๋ค) → ๋ฐ๋ผ์, ํด๋น ํ๋กํผํฐ๋ subview์ ํ๋กํผํฐ๋ฅผ ์ฃผ์ ํด์ผํ ๋ ์ฌ์ฉํ๋ ๊ฑธ ๊ถ์ฅ.
- @StateObject ํ๋ฉด์ด ๋ค์ ๊ทธ๋ ค์ ธ๋ ํญ์ ๊ฐ์ ๊ฒฐ๊ณผ๋ฅผ ์ป์ ์ ์์ → ๋ทฐ๊ฐ ์ฌ์์ฑ ๋์ง ์๊ธฐ ๋๋ฌธ์ ๊ฐ์ ์ด๊ธฐํ ์ํค์ง ์๋๋ค.
References
https://www.avanderlee.com/swiftui/stateobject-observedobject-differences/
@StateObject vs. @ObservedObject: The differences explained
@StateObject and @ObservedObject have similar characteristics but differ in an important way which can lead to unexpected bugs.
www.avanderlee.com
@Published
- ์ปด๋ฐ์ธ ์์ฑ
- ObservableObject์์ ์์ฑ์ ์ ์ธํ ๋ ์ฌ์ฉํ๋ PropertyWrapper.
- $ ํ์๋ฅผ ํตํด ๊ฐ์ ์ ๊ทผํ ์ ์์.
- Observable ํ๋กํ ์ฝ์ ์ค์ํด์ผํ๋ค.
- @Published๋ก ์ ์ธ๋ ์์ฑ์ ํด๋น ์์ฑ์ ๊ฐ์ด ์
๋ฐ์ดํธ ๋ ๋๋ง๋ค ๋ทฐ๋ฅผ ์๋์ผ๋ก ์
๋ฐ์ดํธ ํ๋ค.
- ObservableObject (ํด๋์ค) ์ ์ฃผ๋ก ํจ๊ป ์ฌ์ฉํ๋ฉฐ, ์ ์ธ ์์ฑ์ด ๋ณ๊ฒฝ ๋ ๋๋ง๋ค ํผ๋ธ๋ฆฌ์ ๊ฐ ์๋์ผ๋ก ํธ์ถ๋์ด ๋ทฐ๋ฅผ ์ ๋ฐ์ดํธ ํ ์ ์๋ค.
- ๋์ , ํด๋น ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ๋ ๋ชจ๋ ๋ทฐ๋ ๋ณ๊ฒฝ์ฌํญ ๋ฐ์์ ํ๊ธฐ ์ํด ๋ทฐ๋ฅผ ๋ค์ ๊ทธ๋ฆผ. → @Publisehd๋ฅผ ์ฐ์ง ์๋ ํ ๋ณ๊ฒฝ์ฌํญ์ด ์์ ๋ ๋ทฐ๋ฅผ ๊ฐ์ ๋ก ๋ค์ ๊ทธ๋ฆฌ์ง ์์.
References
https://clamp-coding.tistory.com/513
SwiftUI - @ObservableObject, @ObservedObject, @Published, ObjectWillChange
@ObservableObject @ObservedObject๋ ObservableObject ํ๋กํ ์ฝ์ ์ค์ํ๋ ๊ฐ์ฒด๋ฅผ ๊ด์ฐฐํ๋ ๋ทฐ ๋ด์์ ์ฌ์ฉ๋๋ ํ๋กํผํฐ ๋ํผ์ ๋๋ค. ObservableObject๋ Classํํ๋ง ๊ฐ๋ฅํฉ๋๋ค. Structํํ๋ ๋ถ๊ฐ๋ฅํฉ๋๋ค. class
clamp-coding.tistory.com
https://green1229.tistory.com/228
Combine - ObservableObject / @Published / @ObservedObject
์๋ ํ์ธ์. ๊ทธ๋ฆฐ์ ๋๋ค๐ข ์ด๋ฒ ํฌ์คํ ์์๋ ObservableObject๋ผ๋ ํ๋กํ ์ฝ์ ๋ํด ํ์ตํด๋ณด๊ฒ ์ต๋๋ค๐๐ป ์ถ๊ฐ๋ก ์ด์ด์ ธ์ @Published์ @ObservedObject๋ ๋ณด์์ฃ ! ์์ฆ ๊ฐ์ธ์ ์ผ๋ก ๋ง์ด ๋ฐ๋น ์ ธ์ ๋ธ
green1229.tistory.com
https://www.hackingwithswift.com/quick-start/swiftui/what-is-the-published-property-wrapper
https://developer.apple.com/documentation/combine/published
Published | Apple Developer Documentation
A type that publishes a property marked with an attribute.
developer.apple.com
@environmentObject
- ์ฑ์ ์ ์ญ ์ํ๋ฅผ ๋ํ๋ด๋๋ฐ ์ฌ์ฉํจ
- ๋ทฐ ๊ฐ ๋ฐ์ดํฐ๋ฅผ ๊ณต์ ํ ๋ ์ฌ์ฉํ๋ค.
- parent ๋๋ ancestor ๋ทฐ์์ observable ๊ฐ์ฒด๋ก ์ ์ํ๋ฉฐ, subview์ ์ ๊ทผ์ด ๊ฐ๋ฅํ propertyWrapper์ด๋ค.
- ํ๋์ ๋ณ์๊ฐ ์ฌ๋ฌ ํ์ ๊ณ์ธต์ ๊ฑธ์น ๋, state-binding ๊ตฌ์กฐ ๋ณด๋ค๋ environmentObject๋ฅผ ํ์ฉํ๋๊ฒ ์ข๋ค.
References
https://developer.apple.com/documentation/swiftui/environmentobject
EnvironmentObject | Apple Developer Documentation
A property wrapper type for an observable object that a parent or ancestor view supplies.
developer.apple.com
https://ios-development.tistory.com/1161
[iOS - SwiftUI] @EnvironmentObject ์ฌ์ฉ ๋ฐฉ๋ฒ (๋ทฐ ๊ฐ ๋ฐ์ดํฐ ๊ณต์ ๋ฐฉ๋ฒ, .environmentObject())
๋ชฉ์ฐจ) SwiftUI์ ๊ธฐ๋ณธ - ๋ชฉ์ฐจ ๋งํฌ @EnvironmentObject ๋? parent ๋ทฐ์์ ์ ์ํ observable ๊ฐ์ฒด์ด๋ฉฐ, subview๋ค์ด ์ ๊ทผ ๊ฐ๋ฅํ ํ๋กํผํฐ๋ก property wrapper ํ์ ์ฆ, parent ๋ทฐ์์ subview์ ํน์ ์ํ๋ฅผ ์ฃผ์ ํ์ฌ, sub
ios-development.tistory.com
@Bindable
- iOS 17+
- Observable์ ์ค์ํ๋ ์ค๋ธ์ ํธ์ ๋ํด @Binding์ด ํ์ํ ๊ฒฝ์ฐ์๋ง @Bindable์ ์ฌ์ฉํ๋ฉด ๋๋ค. (state, binding...๋ฑ๋ฑ ๋ค ์๋ ~ ํ ์ ์๋ค.)
https://www.hohyeonmoon.com/blog/swiftui-data-flow
- ObservableObject →@Observable
https://www.donnywals.com/whats-the-difference-between-binding-and-bindable/
What’s the difference between @Binding and @Bindable – Donny Wals
With iOS 17, macOS Sonoma and the other OSses from this year’s generation, Apple has made a couple of changes to how we work with data in SwiftUI. Mainly, Apple has introduced a Combine-free version…
www.donnywals.com
- Apple has introduced a Combine-free version of @ObservableObject and @StateObject which takes the shape of the @Observable macro which is part of a new package called Observation.
- On iOS 17 we have access to the @Observable macro which doesn't enable us to create bindings in the same way that the ObservableObject does. Instead, if our @Observable object is a class, we can ask our views to make that object bindable.
→ iOS 17 ๋ถํฐ @Observable ์ด๋ผ๋ ๊ฒ ์๊ฒผ๊ณ , ์ด ๊ฐ์ฒด๋ ํด๋์ค. ํด๋์ค์์ bindingํ๊ณ ์ถ์ ๋ ๊ฐ๋ฅ์ผ ํด์ฃผ๋ ๊ฒ bindable์ด๋ค.
→ ์ธ์ ์ฌ์ฉํ๋๋ฐ? ๋ทฐ๊ฐ Observable ๊ฐ์ฒด๋ฅผ ๊ฐ์ง๊ณ ์์ง ์์ ๋ @State๋ฅผ ์ฌ์ฉํ๋ ๊ฑด ์ ์ ์น ๋ชปํจ. Bindable์ ์ด๋ฐ ๊ฒฝ์ฐ์ ๊ฐ์ฒด์ ํ๋กํผํฐ์ ๋ฐ์ธ๋ฉ์ ๋ง๋ค ์ ์๋๋ก ํด์ฃผ๋ ์ญํ ์ ํด์ค๋ค.
References
https://eunjin3786.tistory.com/582
[SwiftUI] @Observable ๋งคํฌ๋ก (2)
[SwiftUI] @Observable ๋งคํฌ๋ก (1) ์์ ์ด์ด์ง๋๋ค. [ ์์ฝ ] # 1. ์์ ์ ๋ทฐ์์ ์ผ๋ ํ๋กํผํฐ ๋ํผ @State, @Binding, @ObservedObject, @StateObject, @EnvironmentObject, @Enviroment iOS 17+ ๋ถํฐ๋ 4๊ฐ๋ง ์ฐ๋ฉด ๋จ @State, @Bindin
eunjin3786.tistory.com
https://developer.apple.com/documentation/swiftui/bindable
Bindable | Apple Developer Documentation
A property wrapper type that supports creating bindings to the mutable properties of observable objects.
developer.apple.com
@Binding vs @Bindable
์ฐจ์ด์
- @Binding์ ๋ทฐ์ ์ผ๋ถ ์ํ๊ฐ ๋ค๋ฅธ ๋ทฐ์ ์ํด ์์ ๋๊ณ ์์ผ๋ฉฐ, ํด๋น ๋ฐ์ดํฐ์ ๋ํ ์ฝ๊ธฐ ๋ฐ ์ฐ๊ธฐ ์ ๊ทผ๊ถํ์ ๋ชจ๋ ๊ฐ์ง๊ณ ์์์ ๋ํ๋ผ ๋ ์ฌ์ฉํ๋ค.
- @Bindable์ Observable ํด๋์ค๋ฅผ ์์ ํ ํ๋กํผํฐ์ ๋ํ ๋ฐ์ธ๋ฉ์ ์์ฑํ ์ ์๊ฒ ํด์ค๋๋ค. Observable์ ์ค์ํ๋ ํด๋์ค์๋ง ์ ํ๋๋ฉฐ, Observable ๊ฐ์ฒด๋ฅผ ๋ง๋๋ ๊ฐ์ฅ ์ฌ์ด ๋ฐฉ๋ฒ์ @Observable ๋งคํฌ๋ก๋ฅผ ์ฌ์ฉํ๋ ๊ฒ.
๊ณตํต์
- ๊ฐ๋ ฅํ ๋ฐ์ดํฐ ๊ณต์ ๊ธฐ๋ฅ์ ๊ฐ๋ฅํ๊ฒ ํ๊ธฐ ์ํด ์กด์ฌํ๋ค๋ ๊ณตํต์ ์ด ์์.
References
https://www.donnywals.com/whats-the-difference-between-binding-and-bindable/
What’s the difference between @Binding and @Bindable – Donny Wals
With iOS 17, macOS Sonoma and the other OSses from this year’s generation, Apple has made a couple of changes to how we work with data in SwiftUI. Mainly, Apple has introduced a Combine-free version…
www.donnywals.com
์ถ๊ฐํ์ต
The Source Of Truth
- ์๋ณธ ๋ฐ์ดํฐ์ ๋ณํ์ ๋ฐ๋ผ ๋ทฐ๋ฅผ ๋ ๋๋ง ํด์ฃผ๊ธฐ ๋๋ฌธ์ ๋ฐ์ดํฐ์ ์ผ๊ด์ฑ๊ณผ ์ ํ์ฑ์ ์ ์งํด์ผํจ.
- ์ํ๊ฐ์ด ์ฌ๋ฌ ๊ณณ์์ ๋ณต์ฌ๋๊ณ ์ฌ์ฉ๋๋ฉด ์ฌ์ด๋ ์ดํํธ ์ํ์ด ์์. (๋ฌด์๋ณด๋ค SwiftUI์ UI๋ ๋ฐ์ดํฐ ๋ชจ๋ธ์ ๋ฐ์ธ๋ฉ ๋์ด ์์ด์ ๋ณํ๊ฐ์ ๋ฐ๋ผ ์๋์ผ๋ก ๋ณ๊ฒฝ ๋๊ธฐ ๋๋ฌธ์ ํด๋น ๊ฐ๋ ์ ์ธ์งํ๊ณ ์๋ ๊ฒ์ด ์ค์ํจ)
- SwiftUI์ UI ์ ๋ฐ์ดํธ๋ ์์๋ทฐ์ source of truth์ธ ๋ฐ์ดํฐ๋ชจ๋ธ (e.g. @State…)์ ๋๊ณ , ํ์๋ทฐ์๋ ๊ฐ์ ๋ณํ๋ฅผ ์ ๋ฌํด์ฃผ๋ ๋ฐฉ์.
- ๋ฐ๋ผ์, swiftUI์์ ๊ฐ์ฅ ๊ธฐ๋ณธ์ด ๋๋ ์ ์ ๋ ‘๋ทฐ๋ a single of source๋ฅผ ๋ฐ์ํ๊ณ ์๋ค’์ ๊ฐ๋ค.
- SwiftUI์์ State์ Binding์ SSOT๋ฅผ ์ ์งํ ์ ์๋๋ก ํ๋ propertyWrapper์ด๋ค.
- ์?! → @State๋ฅผ ํตํด ํ๋์ single source of truth๊ฐ ์์ฑ ๋๊ธฐ ๋๋ฌธ!
- UIKit์์ ๋ฐ์ดํฐ ๋ณํ์ ๋ฐ๋ผ ๋ทฐ ์ ๋ฐ์ดํธ๋ฅผ ๊ด๋ฆฌํ๋ ๋ก์ง ๊ตฌ์ฑ์ด ๊น๋ค๋ก์ ์ผ๋, SOT๋ฅผ ํ์ฉํ๋ฉด ๋ฐ์ดํฐ๋ชจ๋ธ๊ณผ ๋ทฐ ์ฌ์ด์ ์์กด๊ด๊ณ๊ฐ ์๊ธฐ๊ธฐ ๋๋ฌธ์ ๋ฐ์ดํฐ ์ํ๊ฐ ๋ณํ๋ฉด ๋ทฐ๋ฅผ ์๋์ผ๋ก ๋ ๋๋ง ํ ์ ์๋๋ก ์ ์ํ ์ ์๊ฒ ๋จ.
- SwiftUI์์ State์ Binding์ SSOT๋ฅผ ์ ์งํ ์ ์๋๋ก ํ๋ propertyWrapper์ด๋ค.
Reference
https://medium.com/@eung7/the-source-of-truth-in-swiftui-state-observableobject-1421b8f80f54
The Source Of Truth in SwiftUI, State ObservableObject
SwiftUI์์ UI์ ๊ด๋ จ๋ ๋ฐ์ดํฐ ํ๋ก์ฐ์์ ๊ฐ์ฅ ์ค์ํ ๊ฒ์ Source Of Truth์ ๋๋ค.
medium.com
https://80000coding.oopy.io/0930a96d-aaa5-4831-9974-03615ae1d6b6
SwiftUI) Source of Truth๋???
Source of Truth๋???
80000coding.oopy.io
[SwiftUI] ํท๊น๋ฆฌ๋ State, Binding, ObservedObject, EnvironmentObject ์ด์ ๋ฆฌ
SwiftUI์์์ Single Source of Truth(SSOT, ๋จ์ผ ์ง์ค ๊ณต๊ธ์)๋ ๋ฐ์ดํฐ์ ์ผ๊ด์ฑ๊ณผ ์ ํ์ฑ์ ์ ์งํ๊ธฐ ์ํ ์ค์ํ ๊ฐ๋ ์ด๋ค. ์ ๋ณด ์์คํ ์ ๋ํ SSOT(Single Source Of Truth) ์ํคํ ์ฒ ๋๋ SPOT(Single Point Of Tr
kimhaeun.com
'๐ Dev > SwiftUI' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[SwiftUI] Navigation 1 (0) | 2024.09.24 |
---|---|
[SwiftUI] scrollTargetLayout๊ณผ ScrollTargetBehavior (0) | 2024.08.16 |
[SwiftUI] Frame (0) | 2024.07.22 |
[SwiftUI] @State (0) | 2024.05.22 |
[SwiftUI] NavigationView, NavigationStack, navigationTitle (2) | 2024.04.15 |