UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: SwiftUI - List unique identifier

Forums > SwiftUI

I've got a little doubt about the list items in a SwiftUI List.

As far as I understood, a list item should implement "Identifiable" protocol.

I've also seen some examples about lists with the following constructor: List(myCollection, id:\.self){...} where myCollection's items don't conform to Identifiable.

Could somebody explain to me the meaning of \.self ? Does it replace the needs to conform to Identifiable for items in myCollection ? Why ?

Thanks

Giuseppe Rossi

2      

Identfiable structs require a id constant so you dont need to express the id .self when calling it. The id: .self just uses the piece of data itself to identify itself from others. So therefore you dont have to conform to identifiable if you declare the id itself. One useful trick is creating a struct and using UUID()

struct StructName: Identifiable { let id = UUID() var name = "" . . . In this way swift creates a unique id for each piece of data you create. hope this helps, i was very confused about this for a long time too

4      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.