1:01 I'm very curious, why is that range's identity not "stable"? I mean, why is not able to see the range has grown?
@swiftandtips Жыл бұрын
Great question. Check out this link that explains the issue very well: www.hackingwithswift.com/forums/swiftui/compiler-warning-non-constant-range-argument-must-be-an-integer-literal/14878
@danstoian7721 Жыл бұрын
@@swiftandtips Hi! Just for the sake of anyone listening, a small completion to your great video! And I mean it, I love your videos! If one will look into the Apple documentation for the List initializer that takes a Range and content, one will see the range's identity need not be fixed as it's presumed to be a constant range, and thus only read one. What happens here is, when we add a new element, in todos, who's annotated with @State, it will cause the View to re-draw, thus re-creating the List and reading the Range again. While checking the Todo, does not affect the overall list, but only the inner element, as pointed out by the author.
@swiftandtips Жыл бұрын
@@danstoian7721 Great explanation. The problem with the list is that we are using index as ID for SwiftUI. Probably 99% of the time you will be ok, but in some conditions where the sorting find two identical elements, there's no additional criteria to consider to keep one element before other and that could produce a potential inconsistency swapping elements at any new render (or in case of animations). TL;DR: The point of this video was to use a dedicated ID (UUID) for cases like this and do not trust only on Int indices.