ForEach
struct
식별(identified) 데이타의 collection에 따라 뷰를 계산.
Declaration
struct ForEach<Data, ID, Content> where Data : RandomAccessCollection, ID : Hashable
Example
let items = ["a", "b", "c", "d", "f"]
VStack{
ForEach(items, id: \.self) {
Text($0)
}
}
- 객체의 key path는
\로 표시. - \.self : 각 아이템은 자기 값(self value)에 따라 고유하게 식별된다. 첫번째 아이템 식별값은 a, 두번째 아이템 식별값은 b라는 뜻이다.
struct Item {
var id = UUID()
var value: String
}
let items = [Item(value: "a"), Item(value: "b"), Item(value: "c")]
VStack{
ForEach(items, id: \.id) { item in
Text(item.value)
}
}
- 각 아이템은 item.id 값에 따라 식별된다.
- \Item.id 를 생략해서 \.id 로 쓸 수 있다.
Leave a comment