8 Common SwiftUI Mistakes - and how to fix them!

  Переглядів 49,126

Paul Hudson

Paul Hudson

День тому

SwiftUI is a big and complex framework, and although it’s great fun to work with there’s also a lot of scope for making mistakes. In this article I’m going to walk through 8 common mistakes SwiftUI learners make, and how to fix them.
1:48 - Adding views and modifiers where they aren't needed
4:05 - Using @ObservedObject when they mean @StateObject
6:38 - Putting modifiers in the wrong order
12:29 - Attaching property observers to property wrappers
16:20 - Stroking shapes when you mean to stroke their border
19:19 - Using alerts and sheets with optionals
22:36 - Trying to get "behind" their SwiftUI views
25:03 - Creating dynamic views with invalid ranges

КОМЕНТАРІ: 98
@glennadams7047
@glennadams7047 3 роки тому
One of the best teachers ! Knowledgable and great speaker.
@muncho404
@muncho404 3 роки тому
You are simply unbelievable. The amount of knowledge you give to developers is unmeasurable. Thanks a lot Mr. Hudson ♥️
@warrengavin9688
@warrengavin9688 3 роки тому
1. Adding views and modifiers where they aren’t needed - 1:48 2. Using @ObservedObject when you mean @StateObject - 4:05 3. Putting modifiers in the wrong order - 6:38 4. Attaching property observers to property wrappers - 12:28 5. Stroking shapes when you should stroke borders - 16:19 6. Using alerts and sheets with optionals - 19:18 7. Trying to get “behind” your SwiftUI views - 22:36 8. Creating dynamic views using invalid ranges - 25:03
@tahmidazam639
@tahmidazam639 3 роки тому
It’s really clear your main focus is teaching the community and it’s why you are an invaluable asset to any aspiring SwiftUI developer. You provide time stamps and a quick summary at the beginning easing the bug fixing process for all of us. Thank you so much, Paul, many thanks, from Oxford.
@damaniturner6510
@damaniturner6510 3 роки тому
This video was awesome, Paul! Please do more of them.
@stevemurch3245
@stevemurch3245 3 роки тому
Incredibly informative and well paced. Thanks!
@alexandrecarlier
@alexandrecarlier 3 роки тому
Amazingly clear, well-thought and useful video! Thank you soo much 🙏
@daniel-rrr
@daniel-rrr 3 роки тому
Paul, I will be forever grateful to you for the amount of knowledge you share and for being such a great teacher.
@DVMagicStudios
@DVMagicStudios 3 роки тому
I am a long time programmer that keeps coming back to your excellent videos. Good job!
@samr.4692
@samr.4692 3 роки тому
Amazing video! It cleared the air with all the confusions. Thank you so very much!
@Notkdenben
@Notkdenben Рік тому
This is by far the best explanation I’ve come across for @State. I’m very much a beginner to coding in general and began working with SwiftUI late last year until I got frustrated back in February and gave up on it. I picked back up with it in April and understanding that @state means the view owns the data makes that property wrapper so much clearer.
@johnpsuik6322
@johnpsuik6322 10 місяців тому
Thanks for this. As a dev since the 80's, I must say SwiftUI has been one of the trickiest languages I've tried to learn. While crazy powerful, there's so much you need to know up front to make it work. You do a great job explaining things!
@sergeysolomonov3074
@sergeysolomonov3074 3 роки тому
Excellent examples and best videos for study. TYVM, you do great lessons for us!
@ccosta55
@ccosta55 3 роки тому
Excellent video. This is exactly what is needed after one learns the basics and has tried writing some apps. Thank you.
@NoName-yg7te
@NoName-yg7te 3 роки тому
Dear Paul Hudson, I really appreciate your dedication and would like to thank you for your contribution
@olepetersen4460
@olepetersen4460 3 роки тому
Simply great - thanks, Paul!
@vahid_sayad
@vahid_sayad 2 роки тому
That .alert was amazing option that I didn't know that! Thanks Paul
@LGiampouras
@LGiampouras 3 роки тому
Very very useful, made our perception of how they all work much better 👍🏻
@bmhyakiri
@bmhyakiri Рік тому
This channel and the website have been pivotal in my app development 🙌 thanks for the exceptional content.
@JTostitos
@JTostitos 3 роки тому
You’re back!
@lennyedwards62
@lennyedwards62 3 роки тому
Fabulous. Excellent content, clear and presented well. Thank you
@nachoarias7086
@nachoarias7086 2 роки тому
Last week i started with SwiftUI and i feel that is the best decision in a time. I have five partners to teach everything this in the future. Thanks!
@KanyantaMubanga
@KanyantaMubanga 3 роки тому
This is how I know he's a great teacher. After he explains stuff, I always go "Of course, obviously."
@rdtba21qw
@rdtba21qw 2 роки тому
Well that clears up loads of stuff for me Paul - thank you!
@gabrielros1
@gabrielros1 3 роки тому
This video and the idea behind it is great. would love to see more video like this. common / uncommon issues and what is the best way to handle them, for SwiftUI and Combine.
@anotherguycalledsmith
@anotherguycalledsmith 3 роки тому
Yes, actually, Paul is doing the job that I had been expecting from Apple all these years… ;-)
@shivaskanthan6144
@shivaskanthan6144 3 роки тому
Very handy. Thanks Paul!
@prsree
@prsree 3 роки тому
Very useful video, it will definitely save my debugging time around the codes 😃👍
@emanuelecarpigna6327
@emanuelecarpigna6327 3 роки тому
Great work! Probably the best Teacher of swiftUi
@trendz4422
@trendz4422 3 роки тому
Thanks for the video, Paul.
@dimiutube
@dimiutube 3 роки тому
Great video, as usual :-) Thanks for the good advices! I hope, I can remember when I run into these problems :-)
@ChrisHornberger
@ChrisHornberger 2 роки тому
Learnt a couple things. Thanks, man!
@velomehr6845
@velomehr6845 3 роки тому
extremely helpful. finally just found 2 errors in my code with your video. thanks a lot!
@jimmason4723
@jimmason4723 3 роки тому
Thanks Paul great video.
@shonifari5783
@shonifari5783 3 роки тому
This tips will make me remove so much useless repetitive code. Thanks so much!
@pushpankkumar1341
@pushpankkumar1341 3 роки тому
Awesome Content, Thanks Paul
@arnoczkyzoltan6158
@arnoczkyzoltan6158 3 роки тому
I learnt a lot, thank you!
@guitaripod
@guitaripod 3 роки тому
Cheers, Paul. Hopefully you'll do more content like this :D
@StephenTaber
@StephenTaber 3 роки тому
It’s a great day whenever a new Hacking with Swift video comes out! Each one gets me a little closer to wrapping my head around iOS development 🤯
@rudySTi
@rudySTi 3 роки тому
Thank you Paul!
@randomcontrol
@randomcontrol 3 роки тому
Very helpful! Thanks!
@arturofm
@arturofm 2 роки тому
Pufff so much value! Thanks Paul, I was making a mistake on the second one 🤓
@micaelacavallo8801
@micaelacavallo8801 3 роки тому
I love the idea of this video... Reminds me of Pareto 20/80 rule... So, for instance 80% of the problems come from 20% of your mistakes. Identifying those and removing them then leads to disproportionate gains... Very cool frame from which to improve the code, that I haven't seen anywhere else yet. Thanks for sharing
@DmitryKatsubo
@DmitryKatsubo 3 роки тому
Brilliant as always
@timgattermann5974
@timgattermann5974 3 роки тому
You are back on UKposts. Great
@AnwarSaiah
@AnwarSaiah 3 роки тому
Great video, deep explaining. Thanks a million. I've been working my mind around something and have asked about it all over the place with no easy answer so far. I need to build a Text from an array of texts where each time a word is emphasised and should have an image right after, a clickable image that would bring up a meaning on the screen or so. Problem is you cannot concatenate text with a button, or can you? I don't know if you have the time to answer me, but thanks any way for your great videos.
@thephantom1237
@thephantom1237 3 роки тому
amazing as always
@Marvelus2009
@Marvelus2009 3 роки тому
yes modifiers in the wrong order is annoying I wish they add a visual error or something for it. Thank you Paul
@normyg9850
@normyg9850 3 роки тому
Totally awesome! Thanks
@briangayle6892
@briangayle6892 3 роки тому
extremely helpful
@multitudes389
@multitudes389 3 роки тому
great video! There is one thing I wanted to ask! I got the case where I got a SF Symbol and I attach a continuous animation to it like: Animation.linear(duration: speed).repeatForever(autoreverses: false) I get new speed values from my model in the view, but the animation is only changing if I restart the app. It does not update even I print the value of speed and indeed it does change, the animation keeps the original value of speed throughout. What I am doing wrong conceptually here I wonder?
@MauricioChavesDias
@MauricioChavesDias 2 роки тому
This video was awesome.
@itsmikeneas
@itsmikeneas 3 роки тому
Awesome content, thanks 2straws. For the second common mistake, what do you do if you're developing SwiftUI with an iOS13 requirement?
@westerte
@westerte 3 роки тому
Really cool, I almost finished a project written using SwiftUI, but still there is something new)
@giorodri0584
@giorodri0584 3 роки тому
Thanks great video
@briangayle6892
@briangayle6892 3 роки тому
thanks for explain @ObservedObject, and @StateObject, I would use it the other way round or get confused on which one. Maybe you can show next time how different the two function, so people get an detail view of what does what.
@hnunes83
@hnunes83 3 роки тому
Great video. I think I made all the 8 mistakes loll. Thank you so much.
@MauricioChavesDias
@MauricioChavesDias 2 роки тому
Paul, can you please make a video explaining the difference between components and views? I never know when to separate that in my projects. It ends becoming a huge view, full of modifiers.
@RobertHolzapfel
@RobertHolzapfel 3 роки тому
Thanks Paul, you nailed it again, common error number 1 I did a lot with RoundedRectangle, where I only need a color. But one annotation: I think combining PropertyWrappers (like @Published) with Property Observers (like didSet ...) works fine since Xcode 12. Do you disagree?
@FellipeBravo
@FellipeBravo 3 роки тому
Awesome!
@rpreviato
@rpreviato 2 роки тому
You are helping me a log with hackingwithswift, thank you. I'm still a little frustating with SwiftUI, hope that pass very soon with more learning.
@luciferRathbone
@luciferRathbone 3 роки тому
Hi Paul! I have a question for you that " Can we add modifiers for Image in tabItem in SwiftUI?"
@JohnBGibson
@JohnBGibson 3 роки тому
Not understanding the state view was the biggest difficulty when first learning. Noting that regular variables could change, but by themselves, nothing visually would occur. Make sense of course now as you don't want to tax the system unless a state is changed, but when new to swift you learn things like that. For example, why doesn't the button's new set variable not show up in the text field... well you didn't change the view. Do you have any tutorials on sound events in 14, I have been working with sound clips in my apps, but I feel like the way I am doing it is probably using too many resources and makes the app sluggish. Also trying to mix sounds instead of stopping one before starting the next seems tricky.
@Lammax2012
@Lammax2012 3 роки тому
Great! Thanks! =) What about the problem of a huge EnvironmentObject (many Published vars)? Also, what about navigation in complex project, when some screens could lead to several other screens & there are several UI flows in UX? What about design architectures: MVVM, MVI? What about Coordinator pattern for SwiftUI project with custom NavigationStack & container views (when we just switching some state & in switch block dynamically choosing which view to show)? =))))
@MarkVolkmann
@MarkVolkmann Рік тому
Around 21:40 you show how to display an alert using `.alert` and the `item` argument instead of `isPresenting`. But that also uses the `Alert` view which is now deprecated. What is the new way to display and alert based on a binding to an optional value instead of a binding to a boolean?
@pinguinchieftain8757
@pinguinchieftain8757 3 роки тому
Hi Paul. I'm stuck with one question. Can you help me? "isEqual"method and operator "===" identical? Which one to use is a matter of taste or are there nuances?
@sergeyjoke
@sergeyjoke 2 роки тому
Thanks
@platin2148
@platin2148 3 роки тому
Really nice what mac do you have to get these quick live views i always have to wait several seconds.
@twostraws
@twostraws 3 роки тому
It's a maxed out 16-inch MacBook Pro.
@nurlanakylbekov7266
@nurlanakylbekov7266 2 роки тому
Woah, I wanna cry. Thank you thank you for explaining @State and @Observed objects . I couldn’t understand why my fetchDat() networking request request and updates again and again every time I came back from navigation view. I was thinking that it’s because of I’m calling networking request in .onAppear()
@recalltechnique1560
@recalltechnique1560 2 роки тому
This may be a stupid question, but since Swift is open source is there a way to see the code for .font and see the details @paulhudson is talking about?
@raysubject
@raysubject 10 місяців тому
that thing where every new view modifier wraps view into another view - doesn’t it have negative impact on performance with more complex UIs ? I’m currenrly thinkimg about approach for complex app which does a lot realtime audio processing (which is done in separate resltime audio thread using mostly plain C code thriugh ObjectiveC wrapper) - i am a bit afraid SwiftUI will have bad impact on performance… is SwiftUI viable to use also for more CPU extensive apps ??
@khaledannajar5110
@khaledannajar5110 3 роки тому
Using @ObservedObject when you mean @StateObject - @ is a good catch
@NeerajKumar-tx1qo
@NeerajKumar-tx1qo 3 роки тому
Please provide tuorial about cognito ios authentication
@rameeksims131
@rameeksims131 3 роки тому
Apple should hire you as the the CTO’s CTO.
@michaziobro5301
@michaziobro5301 3 роки тому
But what to use instead of StateObject in iOS 13 where there wasn’t It and only ObservedObject. ObservedObject seems to be useless for me now
@neiljsmith11
@neiljsmith11 3 роки тому
@ObservedObject still works just fine if your @ObservableObject is injected via the initializer for your view; and a strong reference to that object is held elsewhere. So in Paul's example, if he were to use `@ObservedObject var model: DataModel` as a property of `ContentView`, the data model instance could perhaps be created by whichever struct/class is creating `ContentView` (where it would be marked with @StateObject). The main benefit here is that it would allow you to use a single instance of `DataModel` in multiple views. Hope this helps. See here: gist.github.com/neilsmithdesign/ad9fc1a6f5856fdf79ce77a3ab4e3d5c
@michaziobro5301
@michaziobro5301 3 роки тому
@@neiljsmith11 Yeah but If I cannot use @StateObject like in iOS 13, then I need to pass this reference to inits basically from AppDelegate
@neiljsmith11
@neiljsmith11 3 роки тому
@@michaziobro5301 yes. If you’re supporting iOS 13 then pass by reference via initialiser would be a solution.
@casperes0912
@casperes0912 3 роки тому
So in the case of something like id: \.self; what exactly is the backslash doing? Never quite got that. It's always just been one of those mystical tokens. - I mean, I guess it relates to what the bound of the following .self is, because it's not the encapsulating ContentView struct, but thee aforementioned values in the list, but in like a general abstract sense, what is the \, does it belong just to SwitftUI or is there a general Swift concept that it comes from? Cheers
@twostraws
@twostraws 3 роки тому
Great question! The backslash signals a Swift key path, and you can find out more about them here: www.hackingwithswift.com/example-code/language/what-are-keypaths
@casperes0912
@casperes0912 3 роки тому
@@twostraws Thanks, Paul. Really appreciate it :)
@casperes0912
@casperes0912 3 роки тому
@@twostraws If you squint it almost looks like a form of reflection, but I assume behind the scenes when compiled this just acts a bit like a pointer or something like that
@ZoeSummers1701A
@ZoeSummers1701A 3 роки тому
Can you please do a video about how you came up with that binding code and explain the syntax as it’s confusing as hell. Swift is NOT easy to read or understand.
@anotherguycalledsmith
@anotherguycalledsmith 3 роки тому
Hello, Paul, thanks a lot! Without you, I would be entirely lost. As always, everything just in time ;-) Still just wondering about this strange screen position (@11:17) of x: 150, y: 150…, I would have expected the x-position to be much more far off… Is it, because it takes the middle of the "Hello, World!" string as the position? See you ;-)
@twostraws
@twostraws 3 роки тому
Yes, exactly. I wish this were optional!
@cescofry
@cescofry 2 роки тому
opt+cmd+[ or ] to move a line in Xcode 😉
@AlanW
@AlanW 3 роки тому
This is brilliant, but what I really need to know is what the keyboard shortcut is to restart the canvas preview! Anyone?
@twostraws
@twostraws 3 роки тому
Opt+Cmd+P.
@AlanW
@AlanW 3 роки тому
@@twostraws You are a god amongst men.
@ragrazila
@ragrazila 3 роки тому
what if you are targetting iOS 13 and don't have access to @StateObject?
@phamconghoan1965
@phamconghoan1965 3 роки тому
27:33 Line 16 , why have not self , you can run code ? I can't run code like you if have not self.:(
@9988khan
@9988khan 3 роки тому
23:43 yes you guess right 😅😅🤭
@MrFauad
@MrFauad Рік тому
👍Have some Exta like🙂
@jonathanhirschbaum6754
@jonathanhirschbaum6754 Рік тому
This is beautiful, but your idea of "cross platform" is somehow distorted :D
@guitaripod
@guitaripod 3 роки тому
First
5 Steps to Better SwiftUI Views
31:19
Paul Hudson
Переглядів 54 тис.
24 Xcode Tips in 15 Minutes
15:30
Paul Hudson
Переглядів 72 тис.
Секретная разработка КГБ! Волга «Догонялка» на V8…
1:07:10
Мастерская Синдиката
Переглядів 2,2 млн
The World's Fastest Cleaners
00:35
MrBeast
Переглядів 28 млн
Python's 5 Worst Features
19:44
Indently
Переглядів 22 тис.
Swift API Calls for Beginners (Networking) - Async Await & JSON
25:35
Will ChatGPT Destroy iOS Developer Jobs? A Demonstration
24:05
Sean Allen
Переглядів 153 тис.
Type Erasure In Swift | iOS Development
15:19
iOS Academy
Переглядів 7 тис.
Data Flow in a SwiftUI App
33:15
CodeWithChris
Переглядів 51 тис.
visionOS Fundamentals - Watch before you build for Vision Pro
7:44
Sean Allen
Переглядів 85 тис.
Cách sửa này được không các bạn?
1:00
Cơ Khí Toàn Nghĩa
Переглядів 764 тис.
Зачем вы показываете ноутбук в аэропорту?✈️
0:29
Интел подвинься, ARM уже в ПК!
14:06
PRO Hi-Tech
Переглядів 116 тис.