7 days of WordPress plugins, themes & templates - for free! Unlimited asset downloads! Start 7-Day Free Trial
FREELessons: 30Length: 4.8 hours

Next lesson playing in 5 seconds

  • Overview
  • Transcript


Before we start writing our first demo application, we should take a few moments to discuss how we can document our code. Comments in Swift are mostly the same as in other languages, but there are a couple of basic differences. Let's look at them now.


Now within the Swift programming language like so many other languages, we can deal with some documentation within a code known as comments. Now there's a couple different ways to do comments within Swift and there's some more elaborate versions but it's not really something that I'm going to cover within this particular course. That'll be something that I'll say for another time, but the basic process behind commenting is that there are two different ways to do it. You can do single line comments and you can do multi-line comments. Now those can get a little confusing depending on what sort of experience with other languages you've had before, but it's actually quite simple within Swift. Now when I talk about a single line comment, that's gonna start with to forward slashes. Now anything on this line, after those two forward slashes going to be a single line comment. Single line comment, because now if I were to come down here line and start to write some code you're going to see that I get full IntelliSense and code complete. And that I will start to be able to write my application, but if I were to come up a line here, I would start typing in var or something like that. You're gonna see that it's not going to work the same way, because everything on this line is considered a comment. Now that's a single line comment. Now you can do multi-line comments using single line comments so if you've ever created a new project or if you saw me create that new project in the very beginning of this course. At the very top there's a bit of a documentation block that has some information about the author of the file and the copyright information and the date and all those types of things. And it looks a little something like a bunch of single line comments. So it has something like this, maybe author, Derek Jensen. And maybe some description about it. And so maybe there's a description here, and so on and so forth, and a date. And it just keeps putting these single line comments all back and forth here. Now, You can absolutely do this and you could, I guess, call this entire monstrosity a multiline comment, but it's really not, it's really a bunch of single line comments. And that might sound a little crazy to say a multiline comment is not a bunch of single line comments, but it really isn't. So you can do this. I won't stop you from doing it, but it is a little silly. One thing that is built into Swift like so many other languages is the capability of being able to cut all of that down. And truly just show all of these lines being grouped together as being a bunch of comments together, and the way that we do that is by specifying the forward slash and a star. And then we, anywhere else that we follow up that with a star Forward slash is going to end the comment. Now anything that I put between these two slash comments or these two slash asterisks is going to be a comment. So I could put anything anywhere in here, I could say this is a simple multi line comment. Now, all of this is going to be a comment because it comes after the slash star and before the star slash. Now this is absolutely fine and this may not be a strange thing to you've probably seen things like this before. But one thing that is slightly interesting about Swift as if I were to get a little bit more verbose an I were to come down here maybe start to do another one. And could I said say something like if I really wanted to document the heck out of this I could say something like multi line comments start with the slash asterisk and can span multiple lines. Now you're gonna see here that I'm starting to get a little bit of an error here and what exactly is going on and it's saying that I have an unterminated comment. Well that's because within the world of Swift, I can actually have nested comments. And just because this is all happening within a multiline comment doesn't mean that Swift isn't going to honor this particular comment. So if I wanted to actually do something like this, I can, but I would have to finish it off by saying ends with the star slash. So if I do that, then it's going to work just fine. But just remember that because Swift does accept nested multi-line comments. If I start to open a second one like this, I'm going to have to close it as well. So it's a little bit of a nuance and I honestly haven't had the opportunity or the desire to do comments within my comments but it's just something to kind of be aware of. So now you have the basic understanding of how you can create comments within your code to kind of help document your code to make it a little bit more readable to yourself and to others if it gets a little bit crazy. And that's never a bad thing.

Back to the top