7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial

Next lesson playing in 5 seconds

  • Overview
  • Transcript


In this lesson, we will cover a topic that is either loved or hated by developers: comments. Whether you like to use them or want to banish them from your mind, it is still important to understand how to use comments within Swift.

Related Links


Now that we've begun to introduce the concept of controlling the flow of your application, that will typically add some complexity to your application while looking through it and reading it and seeing what's happening. So, once you start to add some elements of complexity, it's often nice to be able to document your code a little bit. Now depending on what sort of experience you have, you may or may not be a huge fan of doing this, but the option is there for you, especially while you're learning a new language this can prove to be very helpful. But what I'm talking about here is being able to add comments to your code. Now this is not a new concept. And to be honest with you, this is very similar to many other languages. So SWF doesn't deviate very much, but it definitely pays to know how to do it if you're learning this for the first time. There are two different types of comments when you're writing your SWF applications. There are single line and there are multiline. So let's say, I was writing some sort of application and I'm doing, creating some sort of variables and maybe having some control flow. And I'm creating this value and it's gonna be 20. Let's say, for some reason, I needed to maybe document why I'm using the value of 20. Why isn't it 30 or why, why am I using this magic number? So typically, what you would do in this case is you would create a comment and depending on how long that comment is you're probably gonna choose to use either a single or a multiline. A single line comment is designated by two forward slashes. So at this point now, I can say, use 20 to start with. So this is just giving some sort of context around why I picked 20 or maybe I can document this a little bit more. But what if I just wanna throw in really simple comments here and there, a single line, maybe just a few words. I'm gonna use the single line comment with two forward slashes. The second type of comment is gonna be multiline. So, instead of doing single line, I can do multiline. Now yes, I could do a bunch of different lines like this. And technically, this, this will work. I could definitely do this, but it becomes a little difficult and it becomes a little bit more of a pain to have to handle. So, instead of doing that, I can create a multiline comment by introducing a forward slash and a star or an asterisk. Now this begins a multiline comment. So, at this point, I can start writing some very long comments that span several lines. And once I'm done, I can close that out with doing the same thing I did to start the multiline comment, but doing it backwards. So, I can do asterisk, forward slash. Now that's what is going to denote multiline comments and I can have as many lines in here as I want. It doesn't make any difference. This is all going to be one single comment. Now, I don't have to start my comments on the same line as these, slashes and asterisks. I could drop it down and do it this way. I can enclose them on separate lines, that's all fine. I can indent this stuff. I could move this in. I could do whatever it is I wanna do. It doesn't make any difference as long as I have the beginning and ending that match. Now, one other interesting thing though is I can have comments inside my comments, which is kind of an interesting concept. So down in here, I could put another forward slash asterisks and this is going to cause a problem. Because in many other programming languages, what you'll find is that when you do these multiline comments, basically, everything that happens after the slash asterisks or slash star is ignored until it sees the asterisks slash. But in SWF, that isn't the case. So, if I introduce another forward slash star within my multiline comment, as you can see here, I have an unterminated multiline comment. So, I wanna actually have to come in here and put another asterisk, forward slash to terminate the internal one first and then everything would look fine. So that's something interesting to be aware of. Although, you may not spend a lot of time put, putting comments in your comments. It's just something to be aware of. So those are the 2 basic types of comments when we're working in the SWF language. We have the single line with the two forward slashes and the multiline with the forward slash asterisk and make sure that you terminate with an asterisk forward slash.

Back to the top