5 Steps to Better SwiftUI Views

แชร์
ฝัง
  • เผยแพร่เมื่อ 25 ส.ค. 2024
  • In this video I show you five smart ways to make your SwiftUI views simpler and more maintainable.
    - 3:51 Creating properties for your supplementary views
    - 11:05 Breaking larger views into smaller ones
    - 17:18 Getting action code out of your view body
    - 22:40 Creating View extensions for styling
    - 26:50 Using ButtonStyle and other style protocols
    - 30:25 Wrap up and summary
    You can get the project source code, including both before and after versions, from this link: www.hackingwit...
    Sponsored by RevenueCat, who make it easy to add subscriptions to your app. Find out more at www.revenuecat...

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

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

    I’m just started learning. I’m not 100% sure I understand everything, but your energy is really positive and I appreciate that.

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

    So much value in a single video! Amazing job!

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

    What I learned from Paul is the process of doing things when using SwiftUI. @Paul you are one of the masters of teaching Code. #kudos

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

    I always learn the best programming tips and practices with your videos, they are gold.

    • @twostraws
      @twostraws  3 ปีที่แล้ว

      Glad you like them!

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

    I can't upvote this enough!

  • @leifashley
    @leifashley 2 ปีที่แล้ว

    omg thank you for this. I've been looking for good ways to break up toolbars.

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

    The best Swift "how-to". It's gorgeous!

  • @nitesh-maharaj
    @nitesh-maharaj 3 ปีที่แล้ว +3

    This is the difference between knowing how to write code and software engineering. Your code needs to be readable and maintainable. By breaking your views into reusable components, not only will your UI look and work consistently across your app, but it'll save you from duplication. When you need to make a change, make it in a single place.
    You can go further by moving common functionality into packages, which you can just add to your future projects.
    Paul, it would be great to see you take the SOLID principles and translate them into SwiftUI. Since SwiftUI uses more of a functional approach than an object oriented one, not everything will apply. However, this could be a great series.

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

    Thanks for putting this together! You deliver some great videos over the entire scope of micro to macro to best practices and we really appreciate it!

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

    Great stuff mate especially the extension one. We can use XCode's in-built functionality of extract subviews for 2nd step. The most liked part of the video was your dogs though...

  • @KevSmith235
    @KevSmith235 3 ปีที่แล้ว

    You’re amazing. I just want to give you a hug! Thanks Paul, I’m on day #8 of my coding journey and #3 on my 100 days of swiftUI THANK YOU

  • @samr.4692
    @samr.4692 3 ปีที่แล้ว +1

    Greatest tips are always embedded in your tutorials! Thank you very much for sharing.

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

    Wow 😍best practice for better clean code, Thank you 😊

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

    Great video. Thanks for making references to UIKit, made it much easier to understand.

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

    Appreciate it Paul, thanks for all you do!

  • @3rdman99
    @3rdman99 ปีที่แล้ว

    Breaking apart one big complex chunk of code to smaller manageable pieces is a good advice for any kind of coding, not just SwiftUI.

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

    You're the best!! Always bringing some great tips and knowledge to developers of all skill levels!!

  • @ashoth.2934
    @ashoth.2934 3 ปีที่แล้ว +14

    Dear Paul, you’re absolutely amazing! Thank you very much for your priceless tutorials! But I have a question for you. How did you learn all this so very well? SwiftUI is relatively new.

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

    Awesome tips - thank you Paul. Great work

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

    Awesome content, cannot remember the last time I watched 30min straight through on TH-cam but learned a lot, thanks.

  • @wowtheworldchannel
    @wowtheworldchannel 2 ปีที่แล้ว

    Thanks for this awesome video. I do apply most of them regularly but after watching this video i am sure that i’m not doing anything wrong.

  • @thomasipad7719
    @thomasipad7719 3 ปีที่แล้ว

    This will be my new bible. Subscribed 🤗

  • @yahyabn1223
    @yahyabn1223 2 ปีที่แล้ว

    Thank you Paul for great content you teach.

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

    Thanks Paul I learned a lot through 100DaysOfSwift 😊

  • @rameeksims131
    @rameeksims131 3 ปีที่แล้ว

    Thank you Paul for taking the time to teach us Swift and SwiftUI. Great content like always 👍I end up on HackingWithSwift more than StackOverflow and HackingWithSwift is more helpful.

  • @rdtba21qw
    @rdtba21qw 2 ปีที่แล้ว

    Super useful Paul - will certainly help me clean up some of my enormous views. Thanks

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

    Thank you Paul, exactly what should know each developer in SwiftUi

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

    This was so useful for a beginner like me. Thank you

  • @chasemcmullen3443
    @chasemcmullen3443 2 ปีที่แล้ว

    🔥 awesome video!! Thank you 🙏

  • @dickheadrecs
    @dickheadrecs 2 ปีที่แล้ว

    great tips for someone who is coming into swift UI but knows what they’re trying to avoid! that pyramid of hell indentation is brutal 🙈

  • @wendellb.6798
    @wendellb.6798 3 ปีที่แล้ว

    Excellent! I understood most of what you were talking about, even though that's still a bit beyond me, but I grokked.

  • @ajazahmed9586
    @ajazahmed9586 3 ปีที่แล้ว

    Love it...
    Was using couple of them already... Will try to use them all from now on.👍

  • @dharamveergupta1297
    @dharamveergupta1297 2 ปีที่แล้ว

    Amazing Tutorial 👍

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

    Dear Paul, how did you know? And as always, just in time ;-)
    These massive and messy structs and views are not really any better than these “MassiveVCs”…
    Sometimes, this whole mess is getting so complex and nasty that the compiler even starts to claim that it is taking a copy from memory…
    This is exactly the point when it refuses to compile within a considerable amount of time - and eventually fails.
    Of course, Steve would have said “You are stacking it wrong”…
    Time to clean up and have layout and app logic separated.
    Being a mid level SwiftUI programmer by now, I still have difficulties to find best practise advise…
    I do not expect that from Apple (anymore) - but I really appreciate your help and the insights you are giving us.
    Thanks a lot! ;-)

  • @liam52468
    @liam52468 3 ปีที่แล้ว

    Tips 3,4,5 life savers !

  • @bonnydonny
    @bonnydonny 3 ปีที่แล้ว

    Great one! This topic is huge. Well done. Thanks!

  • @0xjorgev
    @0xjorgev 3 ปีที่แล้ว +1

    as usual @twostraws great content!

  • @srbalan1995
    @srbalan1995 3 ปีที่แล้ว

    This is very helpful. Thanks so much Paul 😊

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

    Your videos are awesome like your dogs!

    • @twostraws
      @twostraws  3 ปีที่แล้ว

      Thank you very much!

  • @saskiaa9165
    @saskiaa9165 3 หลายเดือนก่อน

    I love ppl like this. Readability and structure is so important. I'm new to SwiftUI, I find it hard to change my way of thinking from C to Viewmodels. It looks a lot like C# to me, something I also found complicated when your mind thinks C. In your example, if you move code to seperate views (either in the same file or another file), is that part of the code considered a View or a ViewModel? PS love your dogs. :-)

  • @user-ry6ix2mr8y
    @user-ry6ix2mr8y 3 ปีที่แล้ว +1

    Thanks

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

    Great work, as always!!

  • @MrRonanX
    @MrRonanX 3 ปีที่แล้ว

    Great Content!

  • @jordanzang9601
    @jordanzang9601 2 ปีที่แล้ว

    Cute dog and excellent course

  • @returncode0000
    @returncode0000 3 ปีที่แล้ว

    Unbelieveable superb quality videos. Awesome!

  • @pokiishere-sebastian2126
    @pokiishere-sebastian2126 3 ปีที่แล้ว

    Very helpful video! And lovely dogs. 🐶🐶❤️

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

    Awesome video!

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

    very good, thank you.

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

    Great tips. Thanks.

  • @byhabiboff
    @byhabiboff 2 ปีที่แล้ว

    I love you Paul!

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

    Cool stuff. I would just add that if you're going to simplify your code to the point where something significant is just one line then that line should tell the story, which requires good naming practices. For example, at 17:00 you have collapsed the entire list generation into one line of code. But that line is much to generic to have any meaning.
    ForEach(items, content: ListView.init)
    That could literally be anything. By definition it is a list of views comprised of items. It took be a minute to even figure out what this was and I was watching you do it. I would do something like:
    ForEach(locations, content: LocationView.init)
    Otherwise, you've eliminated the signal, as well as the noise. The important thing is that you can see the layout and you're just a right-click away from jumping into the details when necessary.

  • @clayfulgham21
    @clayfulgham21 3 ปีที่แล้ว

    Great Video! Thanks as always!

  • @tonymukiibi2920
    @tonymukiibi2920 2 ปีที่แล้ว

    hi Paul, I loved that application setup you used as a demo, could you please make a separate video to show how to build that app! please

  • @BTFranklin
    @BTFranklin 3 ปีที่แล้ว

    Well, this was simply excellent.

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

    Yes, we enjoyed the video 🙂

  • @pranith2655
    @pranith2655 3 ปีที่แล้ว

    Great Content. Thanks a lot for all your efforts and content. Great contribution to iOS developer community.

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

    Wow, great stuff, Paul. Thanks a lot!

  • @dream_emulator
    @dream_emulator 3 ปีที่แล้ว

    Great tips! Thanks for this. 👍

  • @ChristopherCricketWallace
    @ChristopherCricketWallace 3 ปีที่แล้ว

    a master class

  • @samuelpatrickhenness
    @samuelpatrickhenness 3 ปีที่แล้ว

    This is great. Thank you.

  • @martinkowollik1432
    @martinkowollik1432 3 ปีที่แล้ว

    Thank you so much! This is great!

  • @casadogaspar
    @casadogaspar 3 ปีที่แล้ว

    SwiftUI has this component approach similar with React, that make all more appealing to me.

  • @seungjunlee00
    @seungjunlee00 2 ปีที่แล้ว

    5:35 🤯🤯 my brain had a goosebump saying, what??? This was possible??

  • @SudhanshuSrivastavaIndia
    @SudhanshuSrivastavaIndia 3 ปีที่แล้ว

    I am amazed who the hell downvotes.

  • @gjermundification
    @gjermundification 3 ปีที่แล้ว

    In particular I like the `struct Location { static let example = ... }` in `Location.swift` Thank you!

  • @indritsaveta3259
    @indritsaveta3259 3 ปีที่แล้ว

    Great Job Sir !

  • @bobgodwinx
    @bobgodwinx 3 ปีที่แล้ว

    Thanks for sharing.

  • @peterbodofsnik9429
    @peterbodofsnik9429 3 ปีที่แล้ว

    Nice! Thx you !

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

    It’s so weird to me that #4 is just an extension and not a custom view modifier. I’ve encountered a lot of pain with custom view modifiers and extensions always seem to work out a little better

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

    @Paul Hudson, can you dive into the hate for @ViewBuilder a bit more? From my understanding the @ViewBuilder simply states, "If I can get a view, the use it, otherwise do nothing." And in effect gives us better control over encapsulating our logic with when and what views to present.

  • @thbi
    @thbi 3 ปีที่แล้ว

    Great! Thank you.

  • @emilytomsUA
    @emilytomsUA 3 ปีที่แล้ว

    thanks for sharing!

  • @michaellatta
    @michaellatta 3 ปีที่แล้ว

    You can surround the if with a Group view

  • @ScaryJack
    @ScaryJack 3 ปีที่แล้ว

    I really like it! Although the style extension part sounds like working on CSS files :) But anyway I like it

  • @kishorekankata9369
    @kishorekankata9369 2 ปีที่แล้ว

    Dogs though 😍

  • @miggylol
    @miggylol 2 ปีที่แล้ว

    I like how you split up logic and views and try and not have them put together. However, I’m not the biggest fan of testing implementation details like the toggle button closure you abstracted into it’s own function. I’ve learned to only test behavior of UI and not the implementation of it. It keeps us from having to rewrite tests in the future if we decide to change the implementation. We shouldn’t have to rewrite tests for something that doesn’t change the end functionality. Thought it would be nice to share a different perspective. Great work so far!

  • @TihomirRAdeff
    @TihomirRAdeff 3 ปีที่แล้ว

    This guy is crazy in a good way 😂 25:25

  • @sergehulne
    @sergehulne 2 ปีที่แล้ว

    So, basically, SwitUI can be used as its own kind of « CSS-like » language, in order to factor out presentation code from app logic.

  • @chris_bennett
    @chris_bennett 3 ปีที่แล้ว

    Amazing!

  • @omerersoy1727
    @omerersoy1727 3 ปีที่แล้ว

    Boom!

  • @maynardkeenan2084
    @maynardkeenan2084 2 ปีที่แล้ว

    Does making these things help solving performance issues of views?

  • @markolsson2740
    @markolsson2740 3 ปีที่แล้ว

    Awesome video as usual, first comment :)

  • @gjermundification
    @gjermundification 3 ปีที่แล้ว

    26:00 How would I make different Views for iPad and iPhone? Is `UIDevice.current.userInterfaceIdiom` still the way to go?

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

    Thanks for video, Help me !!!
    Can convert html to Text in SwiftUI?
    Can we have in Text String with two colors?

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

      You can use "+" to concatenate two Texts, like this: 'Text("Blue") .foregroundColor(.blue) + Text("Red") .foregroundColor(.red)'

    • @realgambi
      @realgambi 3 ปีที่แล้ว

      @@andrewb1173 а пример с работы с jsonapi.org/ раскодировка такой структуры есть?)

  • @michaellatta
    @michaellatta 3 ปีที่แล้ว

    The same goes for large monolithic observed objects.

  • @gregohb
    @gregohb 3 ปีที่แล้ว

    Thank you! Is there a way to offload all these bits to separate files and "include" them into the main routine?

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

    Solution num2 you still have logic inside your body
    what's make differences?

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

    Is there a reason to create a text extension rather than create something that returns a customized view? For illustration purposes it's great, but for "real" code is that how you would go it?

  • @casadogaspar
    @casadogaspar 3 ปีที่แล้ว

    The next thing in recruiting, Alignment Test.

  • @rebelsdeveloper367
    @rebelsdeveloper367 3 ปีที่แล้ว

    learning swift ui and making default framework template . yeahh i hate mvc at least somebody.haha penang( from malaysia any way)

  • @regs_j
    @regs_j 3 ปีที่แล้ว

    I have never used Swift at all, some of the reviews suggest the app takes up a considerable amount of space, is this true?

  • @randomcontrol
    @randomcontrol 3 ปีที่แล้ว

    How does ForeEach know to send the item as the named parameter without telling it the name 😲🤯

  • @grenos
    @grenos 3 ปีที่แล้ว

    I think SwiftUI should/would take a few tips from React Native. They're both pretty similar in terms of paradigm/architecture (building reactive software), and many others. "Separation of concerns" where you split views into logic/UI views as much as possibile is a typical RN approach to developing apps. With that being said I definitely see some of the more traditional UIKit techniques staying around on the SwiftUI best practices list.

  • @AlmansorKerroum
    @AlmansorKerroum 3 ปีที่แล้ว

    👍👍👍

  • @f00kwhiteblackracismwarsh07
    @f00kwhiteblackracismwarsh07 3 ปีที่แล้ว

    Please create an IOS developer roadmap for future self taught IOS developer like me.
    I Google search and I can’t get a clear picture of it.

    • @anotherguycalledsmith
      @anotherguycalledsmith 3 ปีที่แล้ว

      ÄhhhHmm… it is “iOS” and forget about Google - otherwise you might end up programming for Androids ;-)

  • @figurich
    @figurich 2 ปีที่แล้ว

    牛逼!

  • @gjermundification
    @gjermundification 3 ปีที่แล้ว

    Lovely steps, splendid! For more stuff relating to step 2 here is my favorite on this topic: th-cam.com/video/1BHHybRnHFE/w-d-xo.html For the steps 3 through 5 I found places to improve, thank you!

  • @zbigbee2760
    @zbigbee2760 3 ปีที่แล้ว

    I like most of your tutorials but I disagree with this one (5 Steps to Better SwiftUI Views). Should your code be taken over by another programmer, or maybe you need to make updates months later after not having seen your code in ages, applying these methods puts far away the code where the action takes place. Which makes debugging that much harder. If you want to modify a list, you have to dissect where the list is getting its data from, what it's doing with the data, how it's showing the data in list form. In other words, you have to be like a detective to figure out how the list is working because the list elements have been scattered hither and thither, it just slows you down and makes things harder. Or when you're looking to change something that a button does, now you have to jump to a couple of different code pages or multiple parts of your code from where the button is. I agree you can encapsulate view clips and just call these, but it should be that simple. Splitting up the parts further just overcomplicates things. Also, some programmers may make the mistake of applying these "optimization" tricks at the start of coding. A big no-no. If the code isn't working yet, you should first figure out the correct logic before optimizing. In the words of Dykstra: "Premature optimization is the root of all evil."