How to comment in Swift
Documenting and commenting is a key aspect of software development, having a good habit of adding comments helps others understand the code better when working with others, and overall makes the testing process much easier to find bugs and create fixes. In this post, we will discuss all 3 types of comments in Swift. (We can use this in Xcode as well)
You can create a single-line comment using the prefix “//” or “///”, and it marks the entire line as a comment, i.e it will not run any code or words written. This can work in any indentation as long as there is no text behind the prefix.
//This is a single line comment. I can write anything and as much as I want in this line only. This won't be commented since it is the next line. This will read the code and throw an error.
///You can also use this to write a single lined comment. This also will not comment this line.
Multi-Lined Comments in Swift
To write multi-lined comments, you need to start using the prefix “/*” and end it with the suffix “*/”. Anything between this will be considered as a comment regardless of the length.
/* This is a multi lined commment. As long as it is between the "/*" and "*/" , you can use as many lines as you want */
Xcode Shortcut key to add comment block in Swift
We can just simply select the block of code that we want to comment out. Then Press
Command + /
Mark Comment Dividers
When working on large projects in Swift, the mark dividers are extremely beneficial as they help create sections between different parts of code, making it easier to navigate between functions. To create a mark divider, simply use the code: “//MARK – ” and then the name of the section.
Commenting is a crucial aspect when programming, and documenting your process is a highly regarded skill incorporating work and educational projects. Using these processes can really make you stand out from others. Want to know more about your version of Xcode? Check out How to check current swift version that you are using