Use a Static Subscript To Access Values On a Type
Daily Coding Tip 188
I have no idea why it’s possible, but you can treat a type similarly to the way you would treat an array. Static properties exist on the type rather than a value of that type, for instance singletons like UIApplication.shared. The important part to notice is the capital U, which proves this is the type itself and not a value.
I decided to make a type called Number that stores three properties about a number. The number is stored as an Int, Double and String. I am using a NumberFormatter to create a String that spells out the number.
In the unlikely event that this process fails the number will be converted to a String without being spelled out, becoming "1" instead of "one".
Finally there is a ContentView here that shows the values from 0 to 9 in a Form.
Notice that the indices from 0 to 9 are created in the ForEach, but there is no array of Number values.
Instead it is possible to pass any Int to Number in the subscript syntax Number[1] and get a value back, complete with the spelled out String provided by the NumberFormatter.




