Swift Optional String Or Empty Extension

Поделиться
HTML-код
  • Опубликовано: 3 фев 2025

Комментарии • 7

  • @Eugene.Berezin
    @Eugene.Berezin 3 года назад +1

    Nice video! A little correction String is a struct not a class.
    Thank you for the video! It’s very useful!

  • @urmilsetia2389
    @urmilsetia2389 3 года назад +1

    Cool. And extension could be extended to cover for sequences.

  • @PlainTrane
    @PlainTrane 3 года назад +2

    Hi Alex,
    Why not use Swift's nil coalescing operator instead?
    e.g.
    let param2 = param2 ?? ""
    It avoids the if-let/else, the extra variable, and the extension.

    • @InsideiOSDev
      @InsideiOSDev  3 года назад

      yep! this extension is just a "nicety" for readability as you can chain things together with it such as:
      if someString.orEmpty().isEmpty
      where if you do coalescing operator you'd have to reserve to:
      if (someString ?? "").isEmpty
      neither is better or worse, just your own preference of readability. The idea for this extension method comes from Kotlin kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/is-null-or-empty.html

    • @PlainTrane
      @PlainTrane 3 года назад +1

      @@InsideiOSDev Hi Alex. Please pardon the delay getting back to you. Things have been busy, and good, here.
      The conventional Swift expression of isNilOrEmpty is someString?.isEmpty ?? truecertainly
      readability in some cases, it would help educate beginning Swift developers on Swift core language features as well as showing the value of extensions. But starting with a C-style if/else (Swift certainly can use if/else, but in this scenario it's an odd use), the beginning developer misses out on the opportunity to learn a core language feature and comes out with an idea that may lead to some odd code just because they don't understand part of the language, which is unfortunate.
      I hope this helps as you formulate your content and wish you the best.

  • @byaruhaf
    @byaruhaf 3 года назад +2

    Thanks for the video also nil coalescing operator works well let unwrappedparam2 = param2 ?? ""

    • @InsideiOSDev
      @InsideiOSDev  3 года назад +1

      true! this extension is just a "nicety" for readability as you can chain things together with it such as:
      if someString.orEmpty().isEmpty
      where if you do coalescing operator you'd have to reserve to:
      if (someString ?? "").isEmpty
      neither is better or worse, just your own preference of readability. The idea for this extension method comes from Kotlin kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/is-null-or-empty.html