GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

SOLVED: Still completely baffled by why ForEach is not designed to handle closed ranges

Forums > SwiftUI

I read online that it has something to do with Range being allowed to be an empty range and ClosedRange having to have at least one value, but that doesn't make sense: if I have a range 0..<100, then 0 must be in the range—it's the left bound—so it can't be empty (and if I have a range 0...100, then there must be at least two values, because both 0 and 100 are boundary values.

Either way, I just don't understand why ForEach wasn't created to handle ClosedRange in the first place. Why design it that way when 0...99 makes so much more intuitive sense than 0..<100.

🧐🤨🤯😭

   

Im sure Apple have their reasons. They are very smart people that work on improving their products and software 24/7.

Can you be more elaborate on your end goals?

   

It's because Range can represent an empty interval, but ClosedRange cannot. So you can do a ForEach with an empty collection of Views to display (meaning you won't be showing anything) using a Range but you cannot do the same if you were using a ClosedRange.

0...0 contains 0

0..<0 does not contain 0.

1      

Hacking with Swift is sponsored by Alex.

SPONSORED Alex is the iOS & Mac developer’s ultimate AI assistant. It integrates with Xcode, offering a best-in-class Swift coding agent. Generate modern SwiftUI from images. Fast-apply suggestions from Claude 3.5 Sonnet, o3-mini, and DeepSeek R1. Autofix Swift 6 errors and warnings. And so much more. Start your 7-day free trial today!

Try for free!

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

You are not logged in

Log in or create account
 

Link copied to your pasteboard.