Understanding Swift Subscripts

preview_player
Показать описание
In this video, I want to introduce you to swift subscripts
I will step you through the basics as they apply to arrays and dictionaries so that you get a general understanding off what subscripts are and how they are used with Swift collections.
I will then show you how you can create your own custom subscripts and in the process share how you can use subscripts to make your code safer and more generic.
We will finish this by creating two rather useful extensions. One for String and one for Arrays and both are extensions using subscripts.

If you want to support my work, you can buy me a coffee :)

Table of Contents:
0:00 Introduction
1:23 The Basics
7:06 Custom Subscripts
17:58 Subscripts in Extensions

Starter Project Files:
(If you are having problems downloading zip files, use Safari. Chrome restricts zip files by default)

Completed Project:

Other References:
Stewart Lynch's YouTube Channel:
Stewart Lynch on Mastodon:
Stewart Lynch on Twitter:
Stewart Lynch on LinkedIn
Рекомендации по теме
Комментарии
Автор

Wow. Hadn't seen that 'subscript' thing before. You have given me some valuable information that I will be sure to use in my work going forward.

cruiseqe
Автор

This was great. Exactly what I needed to get my mind around subscripts!

robertdavis
Автор

Hahaha, I remember first watching your values 2 years ago. I was a terrified iOS Developer. Now I'm a Senior Engineer and still amazed by your explanations of videos. I was wondering did you ever do a comprehensive video on opaque type values?

jaydavis
Автор

Really nice video. Learned a lot of new tricks here.

Another trick you can use for your out of bounds guard checks is to use a range contains check, instead of two comparable checks. Example: else { return }. If you want to get fancier you can use the pattern match operator. O..<self.count ~= index else { return }. I think it’s the more Swift way of writing it :)

thatDIYlife
Автор

Thanks for the excellent explanation! I would love to see a video about dynamic member lookup 😉

andrs
Автор

Very helpful Stewart! Do you have any videos on implementing ads to monetize an app? I’d love to hear your thoughts and see your process on this topic.

chaordo
Автор

Hey, would it be possible to have a throwing subscript so instead of returning an optional element, it could be caught in a try catch block? Thanks!

trevorschmidt
Автор

Hi Stewart, can you share with information what magic shortcut you used to select 'str' keywords in 'Subscripts in Extensions' section of this video?

maartinwar
Автор

I am getting a strange error in the playground in line 8 of the basics page: 
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BREAKPOINT (code=1, subcode=0x18bcdc794)
frame #0: libswiftCore.dylib`closure #1

robertdavis
visit shbcf.ru