@ObservedObject
, @State
, these things we called property wrappers.we kick off an animation, and we want to track the end point of the animation. But animation clearly a very temporary thing going on inside the View. @State
is for that sort of storage.
it replaces your var in your View with a pointer to some space in memory. that space in memory actually lives for as long as the lifetime of your View, not the life time of your View struct. When we use the phrase "lifetime of your View", we mean the lifetime of its body on screen. So as long as the body of your View is onscreen somehow, the thing your @State
points to will stay around and when the View struct itself is getting desroyed and rebuild, it's always getting re-pointed back to that. So you can rely on your @State
staying the same and living as long as your body is on screen somewhere.
- When we put
@State
before a property, we effectively move its storage out from our struct and into shared storage managed by SwiftUI.@State
should be used with simple struct types such asString
,Int
, andarrays
, and generally shouldn’t be shared with other views.- If you want to share values across views, you should probably use
@ObservedObject
or@EnvironmentObject
instead
(HackingWithSwift) What is the @State property wrapper?
what access control is all about: protecting your internal data structures from other code looking at it or modifying it.
indexOfTheOneAndOnlyFaceUpCard
這筆資料事實上已經被記錄在 cards: [Card] 裡面。
如果不注意,之後可能會導致兩邊資料不同步,這樣不好。看我們把它改成 computed properties。
使用高階函數陳述,即 functional programming? (保留)
強調這東西跟 computed property 不一樣
Container View
s "offer" space to the View
s inside themView
s then choose what size they want to beContainer View
s then position the View
s inside of themContainer View
s choose their own size as per #2 above)(白話)
Image
(it wants to be a fixed size)Text
(always wants to size to exactly fit its text)RoundedRectangle
(always uses any space offered)Text 為什麼是 slightly more flexible?
Spacer(minLength: CGFloat)
這個東西沒事就會吃掉幾乎所有的空間
Divider()
這個東西就只會盡可能只吃掉最小的空間
用 leading, trailing 是因為 right-to-left 語言也可以受惠。
LazyHStack and LazyVStack
ScrollView
use GeometryReader
to calculate the font size of the text on the card view
除此之外,本章改動的內容多是 access control, naming, Swift Type Inference 等與 SwiftUI 不直接相關,故省略。
@ViewBuilder
@ViewBuilder
is just a list of Viewslet
s