Subscripts in macdown
Note that those methods don’t actually exist as we now have subscripts in Swift to do the same.
Subscripts in macdown code#
Those methods can easily make your code look a lot less clean: t("Henk", forIndex: 0)Īt(30, forKey: "Antoine") Without those subscripts, you would likely access the same through defined methods on the array or dictionary. The same subscript can also be used to add new values: array = "Henk" Or to access elements from a dictionary: var agesDictionary = It’s commonly used throughout the standard Swift library, too, for example, to access elements from an array: var array = It can be defined in classes, structures, and enumerations to allow quick access to elements from a certain type. What Is a Subscript?Ī subscript defines a shortcut to elements of a collection, list, or sequence.
However, it’s less likely that you also defined your own custom subscripts while those can be extremely useful as well. It’s likely that you’ve already been using subscripts to, for example, get elements from an array or dictionary. You can use subscripts to set and retrieve values without exposing the inner details of a certain instance.Īn instance can define multiple subscripts, and a subscript can have multiple input parameters. Learn all about static subscripts, default ones, and more Photo by Dave Francis on UnsplashĬustom subscripts in Swift allow you to write shortcuts to elements from collections or sequences and can be defined within classes, structures, and enumerations.