Custom Subscripts in Swift

Learn how to extend your own types with subscripts, allowing you to index into them with simple syntax just like native arrays and dictionaries. By Michael Katz.

Leave a rating/review
Download materials
Save for later
Share
You are currently viewing page 3 of 3 of this article. Click here to view the first page.

Where to Go From Here?

You can download the final playground using the Download Materials button at the top or bottom of this tutorial.

Now that you’ve added subscripts to your toolkit, look for opportunities to use them in your own code. When used properly, they make your code more readable and intuitive.

That said, you don’t always want to revert to subscripts. If you’re writing an API, your users are used to using subscripts to access elements of an indexed collection. Using them for other things will feel unnatural and forced.

For further information on subscripts, check out this chapter of The Swift Programming Language documentation by Apple.

If you have any questions or comments, please leave them below!