Leave a rating/review
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 common mistakes that sometimes stump beginners.
So the first is a typo. So sometimes you’re typing a method and you just make a mistake.
So let’s say, I dunno, you typed a I instead of an E there. So you’ll see error pop up when that occurs that says, cannot infer contextual base of reference to member center, which is kind of confusing and this one makes a little more sense to me at least though. Value of type text has no member multiline text alignment.
So putting this into plain English, it means there’s a text field that you’re trying to call a method on and it doesn’t have a method called multiline text alignment. And so when you see that, you’re like, what do you mean it doesn’t have a member multiline text alignment? Oh, I have a typo and you type an e instead.
Another common mistake you might make is case sensitivity mistake. So Swift is case sensitive.
So you notice here that it has line spacing and the S is capitalized. It doesn’t work if you do a line lowercase spacing. You actually get a very similar error where it says value of type some view has no member line spacing. So just capitalize the S and it works.
And this is where auto completion is your friend. If you use auto completion, it’ll help prevent you from making these type of mistakes. Another mistake that’s common is you just might forget to use a closed parentheses somewhere.
Whenever you do an open parentheses, you have to do a close parentheses. And 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 error here. Cannot infer contextual base expected comma insert comma.
And the reason it’s giving you that error is because it is possible for you to have multiple parameters to a method. So you could do perimeter one comma 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 multiple parameters and you’re supposed to be adding a comma.
But unfortunately, sometimes these error messages don’t give you exactly what’s actually wrong. So the best I can tell you is when something, when you see an error like this, just, you know that the error is starting around this line.
So start looking at it as saying, you know, do I have it the way that I meant it to be here? And then you say, Oh, I missed my parentheses and you can add that back in.
Another very common mistake is missing a curly brace. So say you’re messing around and you’re adding things inside the H stack and as you’re copying and pasting things, somehow you miss that closing curly brace there.
So you 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.
It says it’s expecting and curly brace in struct. And it’s because Swift just isn’t sure where the close 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 opening brace, it’ll highlight where it thinks the matching closing brace is.
So if you’re not sure where it’s gone wrong, you can sort of step down through your code to make sure all of the braces are matching up as you expect.
Two more ways I solve this problem are by reindenting all of the code with Control-I.
So, Command-A, then Control-I to reindent.
This makes it super easy to find when there are too many braces or whatever, because you’ll see something like this where there are two at the same indentation level when there shouldn’t be.
The last way I handle this is with code folding.
This is a tool you can turn on in Xcode’s settings. It’s under “Text Editing” - Display - and then check the code folding ribbon.
Now, along the left side of your code there will be ribbons for each section ending with a closing character on it’s own line.
So we can close up the HStack. And the whole VStack!
I 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 everything inside of the 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, the error codes aren’t, sometimes you’re lucky in that they’re really helpful, they narrow it down to exactly.
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 just says, “You probably didn’t mean to do this, but go ahead anyway.”
In my opinion, it is 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 :]