Custom Swift Property Wrappers

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

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

  • @Денис-ж3ф5р
    @Денис-ж3ф5р 3 года назад +3

    I don't get tired rewatching such wonderful videos.

  • @jaydavis6357
    @jaydavis6357 Год назад

    Such Beautiful Code, I literally aspire to write code like you some day! Thank you for the posting the videos about enums. Enums are my bestfriend now are so powerful.

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

    You are the BEST. It's unbelievable how good you are in teaching. I'm a super fan. Thank you so much.

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

      Wow, thank you! It might have something to do with the fact that by the looks of your profile photo, you are closer to my age than you might be to some of the other content providers. Thank you for that lovely comment.

  • @alexsal6069
    @alexsal6069 Год назад

    Thank you, this helped me a lot. I was totally lost with projected values and your code was quite didactic.

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

    Great explanation with nice examples

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

    OMG this guy is a treasure! Thank you a lot for your work!

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

      Thanks. Glad you are enjoying the videos

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

    Your tutorials are the best.

  • @RaviRanjan-xl7fk
    @RaviRanjan-xl7fk 2 года назад

    Another good session

  • @akshaykumar-qj2kx
    @akshaykumar-qj2kx 2 года назад

    nice explanation

  • @shurale85
    @shurale85 2 года назад

    at 07:00 you used additional property text. What is the adv of it? Can we use just wrappedValue? thank you!

    • @StewartLynch
      @StewartLynch  2 года назад

      As you see at 7:52 we do a transformation on that wrapped value where we trim white space and return the text instead

    • @shurale85
      @shurale85 2 года назад

      @@StewartLynch Yes, that's I can't understand) We could return just wrappedValue.trimingCharacters(), could't we? without defining any property. What did I miss?)

    • @StewartLynch
      @StewartLynch  2 года назад

      @@shurale85 If you try that, you will end up with infinite recursion since you will be constantly modifying the wrapped value. Take a look at the error you get when you try to do that,

    • @shurale85
      @shurale85 2 года назад

      @@StewartLynch ah. Thank u very much for your patient and useful content!

  • @StanislavDuksov
    @StanislavDuksov Год назад

    For example, I would like to use ready @AllCaps and @Trimmed property wrappers for one var Name. How can I do that?

  • @Mahadev-x7u
    @Mahadev-x7u 3 года назад +1

    Last example is cool.. also generic. Thank you so much. Do you have some videos where we use custom property wrappers in real project?

  • @Денис-ж3ф5р
    @Денис-ж3ф5р 3 года назад

    I've got a silly question. How to use two propertyWrappers on one property.? By way of example, @State @Blablabla var num

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

      Give me an example of what you are trying to do that would required two property wrappers

    • @Денис-ж3ф5р
      @Денис-ж3ф5р 3 года назад

      @@StewartLynch okay, I will write a clear piece of code tomorrow to avoid ambiguity. Thank you in advance.

    • @Денис-ж3ф5р
      @Денис-ж3ф5р 3 года назад

      @@StewartLynch never mind. I figured it out on my own. Thank you any way.

  • @bry2k
    @bry2k 4 года назад

    Why are the initializers required in some cases but not others? And then, can you explain at a deeper level what the property wrapper is actually doing when invoked? Is this basically equivalent to immediately calling a function on a new object? The thing I am finding hardest about Swift is that there is so much black-boxed "magic" scattered throughout the language. All these features exist that involve decorator keywords, so I end up writing code the 'hard' way because I simply don't know these features exist. Also, the part where you have to set a default value arbitrarily on the line where the property wrapper is invoked seems like it would be very unintuitive to someone else reading the code. I don't like how that looks - and it leads back to my trying to understand why the initializers requirements are the way they are in some of these examples.

    • @StewartLynch
      @StewartLynch  4 года назад

      The initializer is required any time you have an additional properties or if you are going to be transforming the wrappedValue. Since these are structs, we want to ensure that all properties are initialized. In my examples, the only time I do not have an initializer is when I am accessing the projected value (Example 1). In this case we do not need to initialize the wrapped value since it is never accessed, We only access the actual property and its projected value.
      As for setting a default value, I agree that it is not intuitive, but it will be overridden anyway when an instance of the struct is initialized. PropertyWrappers are indeed a bit confusing. By the sounds of it, I have not done a particularly good job explaining them. I will see if I can put something else together in the future that will clarify more.

    • @Денис-ж3ф5р
      @Денис-ж3ф5р 3 года назад

      structs don't require an initializer unlike classes, however you can utilize an initializer for a struct if you need. In all of examples initializers are a must because you want your functionality to be implemented as soon properties get any value. Thus, if the author of the channel haven't used initializers for structs, he wouldn't have got the expected result on the property from the first initialization. Sound misleading , I know, but you have to practise to understand better. I , for one , always rewatch videos to get it. And that's no hard way, that's a necessity.

    • @Денис-ж3ф5р
      @Денис-ж3ф5р 3 года назад

      BTW, the explanation in the video is nearly flawless, if you find it too difficult , you should take easier classes. Perhaps that was not at your level .

    • @Денис-ж3ф5р
      @Денис-ж3ф5р 3 года назад

      I am reviewing this topic, so I can send you a simple repo if you really want to understand.