Swift Basics: Array vs. Set

preview_player
Показать описание

Most iOS developers instinctively reach for an Array when using collections in Swift. I recommend stopping and thinking if a Set would work better. Sets offer better performance as well as a bunch of powerful methods to compare Sets.

This tutorial was created using Xcode 14 and iOS 16.

My iOS Dev Courses:

iOS Developer Interview Questions Playlist:

Twitter:

Book and learning recommendations that help out the channel if you decide to purchase (Affiliate Links):

Paul Hudson's Hacking With Swift:

Donny Wals - Combine:

Mark Moeyken’s SwiftUI Books:

#swift #softwaredeveloper #iosdeveloper

Timestamps:
0:00 - Is there better?
0:29 - Array vs. Set
1:33 - Time Complexity
2:20 - Code Examples
2:47 - Intersection
3:48 - Subtract
4:44 - Disjoint
5:22 - Union
6:06 - Symmetric Difference
7:03 - Subset & Superset
7:55 - Insert, Remove, Contains
Рекомендации по теме
Комментарии
Автор

Just so you know, what you say about performance is only valid for lookup. There are many use-cases where an array is the better solution, even if you scan through the whole thing. Keyword, cache locality . And if you order the array, you will have awesome performance also with search, and the other set operation you showed.
I would suggest you change the title of the video (but for the youtube algo, that one might be good, also to raise engagement, so maybe leave it for clicks 😁 )

HaraldAchitz
Автор

Hey Sean, nice video!
You mentioned that Arrays in swift have an O(N) lookup complexity.
Arrays actually conform to the RandomAccessCollection protocol, which buys us back the O(1) complexity for a lot of the array's functionality.

jaila
Автор

I think you mistakenly union the same sets instead swiftUIDevs + SwiftDevs.

kavinda_dilshan_paramsoodi
Автор

Never Knew about Sets. I've been using Arrays all this time. Thanks, sean. Valuable video, and I learned something new today.

kavinda_dilshan_paramsoodi
Автор

And then you convert sets to arrays anyway to store in Core Data.

foilhat
Автор

Thanks a lot for this video! It’s was very useful!

ruslangaliev
Автор

Since as Swift devs we're probably doing UI, we should generally be using FetchRequests instead of Arrays or Sets to populate UI. FetchRequests get automatic updates when the database changes and has a sort function built in.

slmille
Автор

Actually reading from an Array (if you have the index) is O(1) which is often the case...

tokero
Автор

I love arrays, back from the old days. But sets are awesome!

Patrick
Автор

Great video, but what will you say about pagination and order? How can I avoid an array?

wewhalea
Автор

Can you make a video about these methods as well ?
lexicographicallyPrecedes

makeIterator
reserveCapacity
underestimatedCount
withUnsafeBufferPointer
partition

Maybe there are some more.

indomitabletr
Автор

What if there are two students with the same name?

arthurdeboer
Автор

Hey Sean, while I agree with you, I use Sets when I wanna store exclusive values like say an Order Number for a Product that is sold, which is unique.

ameyajoshi
Автор

Can you give real life example not using set, say can we use it in tableview or anything similar

abhijeetcherungottil
Автор

It's really great and helpful video, and I just give one suggestion, pls add subtitle if possible, because, YouTube CC is not accurate and my English not fluent, so it's really hard for me to understand you Said, I hope you are getting my point. Anyway very helpful video ❤ 😍

vikasgupta
Автор

Solid info here! Sets have been a rare encounter in all my development days but they are pretty awesome.

DaveJacobseniOS
Автор

Hey Sean! Great content as always!
Got to know about the practical use of the set operators.
Excited to use them from now on 😃
Also wanted to let you know that your fingers might have slipped during demonstrating the union example. 😄
But that's completely understandable...
Looking forward to your next video 😃

MosabbirSadman
Автор

Sean: When you need a collection of something in Swift, you instinctively reach for an array. Don't you?

Me: 👀

JustinWellsDev
Автор

Is the performance difference something you've seen in your apps? Since it's all in memory, I wouldn't think it mattered that much.

ToddHoff
Автор

Set don't work well with SwiftUI though.

xpopcornx