Print

Swift Tutorial: Introduction 04 - Commenting

Written by Paul Napier on . Posted in Swift Tutorials: Introduction

Hello everyone and welcome to the fourth lesson in programming with swift brought to you by MadApper. My name is Paul Napier and in this lesson we are going to cover the concept of commenting.

If you have not come from a strong programming background, then you will learn that Comments are essential when writing your code. I have lost count of the times when either I have written an especially complex piece of code, gone on to write another part or work on an entirely new programme, then had to return to reevaluate the original piece. At this stage, if I haven't written out comments then it becomes a time costly exercise in understanding how the code works. Alternatively, if I am working in conjunction with another developer, then it is just common courtesy to give them the roadmap of how the application works.

So how do I add comments? Well there are two different methods: individual and block comments.

In swift as in objective c and java, individual comments are added by typing // before writing a sentence.

// This is a comment
let count = 10 // This is also a comment var startVariable = 0 for (var a = 0; a<count; a++){     var add = 0; var multiply = 0     add = a+10     if (a%2==1){         var factor = a*a         multiply = factor     }     var calculation = add + multiply     startVariable += calculation } var finalVariable = startVariable

This can be done at any point on a line so here I write it at the beginning, where as here I write it after my statement. What you can see is that everything after the beginning of the comment is effectively ignored by the compiler, however the comment ends as soon as the line breaks.

Block comments are slightly different and are declared using /*. Again they can be declared anywhere on a line, but these can span multiple lines, and only stop being ignored once they are closed with */. If you are used to objective c and java, then you will know that if you open multiple block comments, then as soon as you add that closing component the whole comment is closed. In swift, however, block quotes can be nested within each other, so for every opening declaration there must be a closing one.


// This is a comment
let count = 10 // This is also a comment
var startVariable = 0
for (var a = 0; a<count; a++){
    var add = 0; var multiply = 0
    add = a+10
/* This is a block comment
    if (a%2==1){
        var factor = a*a
/* This is a nested block comment
        multiply = factor
*/
    }
*/
    var calculation = add + multiply
    startVariable += calculation
}
var finalVariable = startVariable


Comments are also useful when debugging your code. For instance if I want to ignore the variable 'multiply' and see my final result without it, then I can comment this out. If I want to remove a block of code I can type /* before it and */ after it or nest it the same way. You can see in each of these instances, that the compiler ignores any code that is commented out and the finalVariable value changes. Try commenting out different lines of code and watch the value of 'finalVariable' in the console.

Ok, so that's enough on comments. In the next lesson we are going to start learning about the basic types. 

Add comment


Security code
Refresh