Your First iOS & SwiftUI App: An App from Scratch

Feb 13 2023 · Swift 5.7, iOS 16, Xcode 14

Part 1: Getting Started with SwiftUI

09. Solve Problems

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 08. Challenge: Add View Modifiers Next episode: 10. Conclusion

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

One of the biggest pitfalls when you're first learning iOS development is understanding what to do if something goes wrong. So next I'll show you how you can solve some common mistakes that sometimes stump beginners. So the first is a typo. Sometimes you're typing a method, and you just make a mistake. You'll see an error pop up. To put it in plain English, it means there's this text view and you're trying to call a method on it, and it doesn't have a method called multi-line text alignment like this. And so when you see that, you might think, Well, what do you mean? It doesn't have a member called multi-line text... Oh, I have a typo. Another common mistake you might make is case sensitivity mistakes. So Swift is case sensitive. So notice here that we have line spacing, and the S is capitalized. It doesn't work if you do line lowercase s spacing. You actually get a very similar error where it says, Value of type 'some View' has no member 'linespacing'. So just capitalize the S and then it works. And this is where auto completion is really your friend. If you use auto complete, it'll help prevent you from making these types of mistakes. Another mistake that's common is you just might forget to use a closed parenthesis somewhere. Whenever you do an open parenthesis, you have to do a closing one. So the errors here get a little weird because you would expect to see something like, missing parentheses, right? But it doesn't really tell you that. It gives you this, Cannot infer contextual base expected comma, insert comma, and the reason it's giving you this error is because it's possible for you to have multiple parameters to a method. So you could do parameter one, parameter two, parameter three, and so on. So Swift is a little confused right now, and it's thinking maybe you're trying to add another parameter, and you're supposed to be putting a comment in there if that's what you're doing. But unfortunately, sometimes these error messages just, they don't tell you exactly what's wrong. So the best thing I can tell you to do is when you're seeing something like this, just know that the error is starting somewhere around this line. So start looking at it saying, do I have it the way that I meant it to be? And then you say, Oh, okay, I missed my parenthesis, and you can just add it back in. Another really common mistake is missing a curly brace, same kind of thing. So you're messing around and you're adding things inside the HStack. And as you're copying and pasting things, somehow you miss that closing curly brace. So you'll get an error again. And this time the error is not even close to where the error is. It's all the way down here with the preview code, and it says it's expecting a curly brace in a strut, and it's because Swift just isn't sure where the closing curly brace is supposed to be. So when it comes to missing curly braces, what I like to do is whenever you type a curly brace or put your cursor next to an open brace, it'll highlight where it thinks the matching brace is. So if you're not sure where it's gone wrong, you can sort of just step down through your code to make sure that all of the braces are matching up as you expect. Two more ways I solve this particular problem are by reindenting all of the code with Control I. So command A, then Control I to reindent. And this makes it super easy to find where there are too many braces. The last way I handle this is with code folding. This is a tool that you can turn on in Xcode settings. It's under Text Editing, Display, and then check the code folding ribbon. Now along the left side of the code there will be ribbons for each section ending with a closing character on its own line. So we can close up the HStack and the whole VStack and use this to check for missing syntax and also just to clean things up and make it easier for me to focus on the code I want to edit. So if there's something you want to apply to everything inside of a Vstack, it's easy now to see where to put that. You can unfold by clicking on the ribbon again or pressing return with the bit inside of the braces selected. So those are some of the common errors you might get. Unfortunately, error codes are kind of a mixed bag. Sometimes they're really helpful, and they narrow it down exactly to what you need to fix. And other times the syntax may seem a little bit cryptic, especially with SwiftUI, but often they'll point you in the right direction and help you figure out what's going on. So far, we've been dealing with errors, which are in red, but sometimes Xcode will give you a warning, which is in yellow. What's the difference? Well, errors are fatal. If you get one, you cannot run the app till the error is fixed. On the other hand, warnings are informative. Xcode is just saying, You probably didn't mean to do this, but you can. In my opinion, it's best to treat all warnings as if they were errors. Fix the warning before you continue, and only run your app when there are zero errors and zero warnings. That doesn't guarantee the app won't have any bugs, but at least they won't be silly ones.