What is the backslash(\) used for in SwiftUI?
In SwiftUI
, the blackslash operator is used to refer keypath to use inside given block.
from apple:
Add the ability to reference the identity key path, which refers to the entire input value it is applied to.
So for example, see this code:
ForEach(["iPhone SE", "iPhone XS Max"].identified(by: \.self)) { deviceName in
LandmarkList()
.previewDevice(PreviewDevice(rawValue: deviceName))
}
here while iterating through array, use the self(here - string) as key
Now take another example: where we use array of objects(not string), now in that case the key which is used as key inside block for iterating is id.
List(landmarkData.identified(by: \.id)) { landmark in
LandmarkRow(landmark: landmark)
}
\.self
it's a identity keypath that apple added for:
Add the ability to reference the identity key path, which refers to the entire input value it is applied to.
More info in proposal.