iOS Cool Stuff – Immutability by default

This is the first entry in the iOS Cool Stuff series. As the opposite of the iOS Pain Points series, it will talk about some of the nifty parts of iOS development – the kind of stuff I wish I had in the .NET world.

One very cool feature of Objective-C is that data structures tend to be immutable by default. Your standard array data structure, NSArray, is immutable. If you want a mutable version, you have to specifically ask for NSMutableArray. This is the way things should be.

In .NET, immutability is a bit harder to come by. Sure, strings are immutable, but what about collections? Sure, you can use the ReadOnlyCollection<T> wrappers, or expose an IEnumerable property, but neither of these options is as slick as simply having built-in immutability.

Leave a Reply

Your email address will not be published. Required fields are marked *