'In SwiftUI, what's the different ForEach without Identifier and with Identifier?
In SwiftUI, in the ViewBuilder, we have to use ForEach instead of for.
However, there are two ways of doing i.e.
ForEach(1...count, id:\.self)
and
ForEach(1..<(count+1))
Other than the syntax being different, is there any different use case for them?
Solution 1:[1]
Joakim has answered your question in the comments.
The second listed alternative can be used as long as the datatype being referenced is Identifiable. That means it is a class or has been declared as conforming to the Identifiable protocol. You can get Identifiable almost for free on your own objects by declaring conformance. The compiler will help you if you say you conform but don't, so that's a good place to start.
From Apple's Online Documentation:
Identifiableprovides a default implementation for class types (using ObjectIdentifier), which is only guaranteed to remain unique for the lifetime of an object. If an object has a stronger notion of identity, it may be appropriate to provide a custom implementation.
The first alternative exists for cases when it doesn't conform to Identifiable.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mozahler |
