Swift Basics: Array vs. Set

แชร์
ฝัง
  • เผยแพร่เมื่อ 28 ก.ย. 2024

ความคิดเห็น • 50

  • @HaraldAchitz
    @HaraldAchitz ปีที่แล้ว +16

    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 😁 )

    • @seanallen
      @seanallen  ปีที่แล้ว +5

      I’m experimenting with playing the “TH-cam Game”. It feels a little dirty, but my past 2 videos have gotten 3x my typical views in the first 24 hours. It sucks, but the game works. 🤷🏻‍♂️

    • @bonifacy9511
      @bonifacy9511 ปีที่แล้ว +1

      Can you give us a real-life example of using Set instead of a array?

    • @torb-no
      @torb-no ปีที่แล้ว +4

      ​@@seanallen If you're that at least include the more nuanced truth in the video itself. This is bordering on spreading misinformation and is making me feel like I ought to double check stuff I've seen in your videos.
      In this case my experience with C++ arrays made it so I already knew. In fact, I was waiting for you to explain that the truth is more complicated… but I never saw it in the video. Disappointing.
      If you click bait (I understand you gotta play the game), you have *got* to include the nunaced truth in the video somewhere.

    • @TeamPuzel
      @TeamPuzel ปีที่แล้ว

      ​@@torb-no It's even more complicated in Swift though, it's best to avoid mentioning performance at all.
      I'd focus on videos about cool features not everyone might have noticed, or things Swift can express more clearly than other languages. Writing performant code in any language requires deep understanding of memory and how code is compiled rather than memorising which features are "faster" and when.

    • @torb-no
      @torb-no ปีที่แล้ว +1

      @@TeamPuzel My understanding is that if your structs are small enough they have some of the C++ aspects.
      But I suppose Copy-on-Write can make it more complicated too.

  • @Patrick462
    @Patrick462 ปีที่แล้ว

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

  • @MosabbirSadman
    @MosabbirSadman ปีที่แล้ว +1

    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 😃

  • @arkemal
    @arkemal ปีที่แล้ว

    I love Sets! Good shout Sean!

  • @vikasgupta6657
    @vikasgupta6657 ปีที่แล้ว

    It's really great and helpful video, and I just give one suggestion, pls add subtitle if possible, because, TH-cam 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 ❤ 😍

  • @NikDany
    @NikDany ปีที่แล้ว

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

  • @ameyajoshi5673
    @ameyajoshi5673 ปีที่แล้ว

    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.

  • @abhijeetcherungottil6703
    @abhijeetcherungottil6703 ปีที่แล้ว

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

  • @ruslangaliev5050
    @ruslangaliev5050 ปีที่แล้ว +1

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

    • @seanallen
      @seanallen  ปีที่แล้ว

      Glad you enjoyed it, Ruslan!

  • @DaveJacobseniOS
    @DaveJacobseniOS ปีที่แล้ว +2

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

  • @slmille4
    @slmille4 ปีที่แล้ว +2

    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.

  • @vongodric
    @vongodric ปีที่แล้ว

    Oh... the date?

  • @victorriurean
    @victorriurean ปีที่แล้ว

    nice

  • @me.rathore
    @me.rathore ปีที่แล้ว +1

    Click bait. 👎

    • @seanallen
      @seanallen  ปีที่แล้ว

      Fair. Title and thumbnail change coming soon.

    • @me.rathore
      @me.rathore ปีที่แล้ว +1

      @@seanallen appreciate that. I rarely put comments on TH-cam, but when i read the title and thought there must be some performance issues related to Arrays. But it’s mostly related to using correct data structure.

  • @foilhat
    @foilhat ปีที่แล้ว +6

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

    • @priceringo1756
      @priceringo1756 ปีที่แล้ว +3

      ...and not just Array but NSArray. I am hoping for more native Swift support from CoreData this WWDC but I am not holding my breath.

  • @jaila17
    @jaila17 ปีที่แล้ว +8

    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.

    • @mivico
      @mivico ปีที่แล้ว +2

      You’re both correct. I believe he was referring to when you don’t know where the item you’re looking for is stored. Then it’s O(N) complexity but if you do know the index that it’s stored in, then it’s O(1) complexity

  • @kdtechniquesofficial6153
    @kdtechniquesofficial6153 ปีที่แล้ว +2

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

  • @ItzTalha
    @ItzTalha ปีที่แล้ว +1

    Hi sir.
    I need pubg mobile redeem code of testflight please help please 🙏

  • @arthurdeboer8846
    @arthurdeboer8846 ปีที่แล้ว +2

    What if there are two students with the same name?

  • @indomitabletr1834
    @indomitabletr1834 ปีที่แล้ว +1

    Can you make a video about these methods as well ?
    lexicographicallyPrecedes
    withContiguousStorageIfAvailable
    makeIterator
    reserveCapacity
    underestimatedCount
    withUnsafeBufferPointer
    partition
    Maybe there are some more.

  • @tokero5199
    @tokero5199 ปีที่แล้ว +2

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

    • @seanallen
      @seanallen  ปีที่แล้ว +1

      Fair point, but I disagree that you often have the index. Most of the time I’ve worked with arrays the data is dynamic and I don’t know the index of an item ahead of time.

  • @edward.narvaez
    @edward.narvaez ปีที่แล้ว +2

    Thank you Sean

  • @kadeus
    @kadeus ปีที่แล้ว +1

    I’m not agree Array is not Set and Set could not be Array

  • @bloq2877
    @bloq2877 ปีที่แล้ว +1

    Great video!

  • @JordanKay-q9y
    @JordanKay-q9y ปีที่แล้ว

    This is not an appropriate title for this video, sorry. Valid uses of `Array` outweigh those of `Set` in a typical codebase by several orders of magnitude. The clickbait title isn’t worth the misinformation.

  • @JustinWellsDev
    @JustinWellsDev ปีที่แล้ว +1

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

    • @seanallen
      @seanallen  ปีที่แล้ว +1

      Lol, we all do it :)

    • @JustinWellsDev
      @JustinWellsDev ปีที่แล้ว

      @@seanallen So true 😂

  • @spelledfunny
    @spelledfunny ปีที่แล้ว

    This name example is a terrible sample case. For example, if you're using Sets to determine permissions, anyone in the company with the same first name will all have the same access to whatever the software is in control of, which could be very undesirable if there was a Sean who was a Jr. Dev and another Sean who was at the C-level.

  • @QwertyNPC
    @QwertyNPC ปีที่แล้ว

    No...I can't take this. Stop this, eat that, 10 fucking ways of doing a "10 fucking ways to do something". You won't miss me, I gotta bail.

  • @kdtechniquesofficial6153
    @kdtechniquesofficial6153 ปีที่แล้ว

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

  • @ToddHoff
    @ToddHoff ปีที่แล้ว

    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.

  • @xpopcornx1747
    @xpopcornx1747 ปีที่แล้ว

    Set don't work well with SwiftUI though.

  • @ivanramirez1465
    @ivanramirez1465 ปีที่แล้ว

    Guilty 😳

  • @bosch5053
    @bosch5053 ปีที่แล้ว +1

    nice video!