Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Swift 2 Blueprints

You're reading from   Swift 2 Blueprints Swift Blueprints

Arrow left icon
Product type Paperback
Published in Oct 2015
Publisher Packt
ISBN-13 9781783980765
Length 276 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Cecil Costa Cecil Costa
Author Profile Icon Cecil Costa
Cecil Costa
Arrow right icon
View More author details
Toc

Table of Contents (10) Chapters Close

Preface 1. Exploring Xcode 2. Creating a City Information App with Customized Table Views FREE CHAPTER 3. Creating a Photo Sharing App 4. Simulating Home Automation with HomeKit 5. Health Analyzing App Using HealthKit 6. Creating a Game App Using SpriteKit 7. Creating an Apple Watch App 8. AVFoundation Index

Some final comments

Working with someone else's code might be a difficult task; you have to figure out what is the logic of the previous developer and how you could change the code to fix a bug, for example. This is the reason why developers must think like a team, act as a team, and work in a team. What does this mean? As a part of our tasks, we should help the next developer by adding comments for them. Swift has its own features based on comments.

Firstly, an interesting part is that Swift allows nested multiline comments. It means that a comment like the following one is not going to stop before println like the other languages do:

        /*
          This code was removed due to it is a bit buggy
          /*
            for i in myArray {
              object.process(i)
            }
          */
          println(myObject.description)
        */

Another good thing that we could do with comments is to mark the current state of development. This is equivalent to the #pragma preprocessor in Objective-C. Let's start with // MARK: -, this allows us to add a title before each section of our class. For example, imagine that we have a big class, so we can use this mark to show which methods belong to the base class, which ones belong to the delegates, and which ones are private methods. In this case, we would have a code similar to the following one:

    // MARK: - Overriden functions
    override func viewDidLoad() {
      super.viewDidLoad()
      //…
    }
    // continue overriding some methods
    // MARK: - Delegates
    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath){
    //…
    }
    // continue writing some delegates methods
    // MARK: - private methods
    private func createTitle(cellNumber: Int) -> String {
    //…

In this case, when you click on the editor's combo box (or press control + 6 as we learned before), you might see a combo similar to the following screenshot:

Some final comments

This kind of mark can also be used to reach a differentiated code (something you might check frequently) but, in this case, it is recommended to use it without the dash character, something like // MARK: Important code, and it can be done anywhere, even in half a function. Removing the dash from // MARK: removes the dividing line in the combo box.

When you have an incomplete code or task, you can mark it with // TODO:, meaning that something is pending in this area. For example, imagine that there is a code that is hardcoded and it is desired to load the information from a configuration file. You might have a code similar to the following:

// TODO: Retrieve this IP address from the configuration file
return "192.168.20.21"

The next mark we have is // FIXME:, it represents that this part of the code has a known issue and it should be fixed. Something like the following:

// FIXME: The app hangs when there is not internet connection
myconnection.retrieveInformationFromServer()

Tip

Try to search for TODO: and FIXME: frequently or do a script that searches for them. There are times when there are too many codes to fix and nobody cares about it.

While you finish this part, don't forget that a well-documented code is a helpful code. When you hold the option key and click over a symbol, you will see some information on that symbol, like the NSURLRequest box shown in the following screenshot:

Some final comments

You can display equivalent information from your classes, methods, and properties by using HereDoc. The idea is very simple; you can take advantage of the comments by starting with ///, followed by its description as follows:

/// A class that represents a garage with its cars
class Garage {

Of course, this format is fine when the description is short. However, if you need to write a more exhaustive description, you'd better use the multiline comments starting with /** and closing it with */.

You can also describe the arguments and the value returned with :param: and :returns: as shown in the following code:

    /**
     Sell a product and remove it from the current stock.
    
     Remember that:
    
     - The product must be available
     - the quantity can't be 0 or negative
    
     :param: product The product you are selling
     :param: quantity Number of units or kilos to sell
     :returns: if the purchase was done correctly
    */
    func sellProduct (product: Product, quantity: Int) -> Bool{

For example, this code will generate the following box:

Some final comments
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime