NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

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

   

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

1      

Hacking with Swift is sponsored by NSSpain

SPONSORED Announcing NSSpain 2020: Remote Edition! An online, continuous conference for iOS developers. We’ll start on Thursday and finish on Friday, with talks, activities, and lots of fun for 36 hours, non-stop. Sound good? Join us!

Find out more

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

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

Not logged in

Log in
 

Link copied to your pasteboard.