https://www.youtube.com/watch?v=vSHbMhy6XH4&list=PLwBf1nbmbSzgC5RLROL798IHwfovDhv16
import SwiftUI
struct Student: Hashable{
let name : String
}
struct ContentView: View {
let student = [Student(name: "Harry"), Student(name: "Hermion")]
var body: some View {
List(student, id: \.self){
student in
Text(student.name)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
사용하려는 struct를 Hashable로 설정하고 let name: String 으로 name을 String으로 받을거라는 것을 알려줍니다
그리고 View에서 구조체와, 그 구조체에서 요구하는 이름을 넣어주면 각자의 id 를 갖고 저장되게 되는데 이는 Hashable이라는 옵션이 붙어 가능합니다
id: \.self에서 Stduent에 저장된 Hashable value를 가져오게 되는 것입니다.
Hashable은 컴퓨터가 모든 property에 hashable value를 지정해주어 같은 값이 들어와도 에러가 뜨지 않게 됩니다.
'Swift > SwiftUI Tutorial' 카테고리의 다른 글
SwiftUI Essentials: Creating and Combining Views (0) | 2023.05.20 |
---|