Mobile Development

Key Programming Concepts

Below is a list of concepts associated with Swift and other similar programming languages. The goal of this chapter is to frame your experience and provide a grounding from which to launch.

Programming Mindset

Below are three concepts to consider when getting into the programming mindset. These concepts will help you stay focused and be productive.

Break it down

It's easier to program when you understand what you are trying to accomplish. What do you want to happen? Can you break it down further?

A Systematic Approach to Solving Just About Any Problem

Having Fun

When you choose what to program, what to build and what to release to the world, realize that it takes time. A lot of time. This is all part of the process. Keep at it, try and have fun. The more experience you have the easier it becomes.

Make it easy

As a developer, you'll spend a lot of time reading code. Making code easy to read helps the process of reading and understanding code. Not just for you, but future you. And for anyone else who might have to read your code, too.

// This is harder to read
let x = 1 + 2
let y = 3 + x + 2
let z = 1 + 1 + 1 + 1 + 1 + 1 + 6 / 2

// This is easier to read
let x = 3
let y = 5 + x
let z = 9

Keep things simple. Follow the above concepts and future you will thank you with a high five.

Key Programming Concepts

Now we're going to introduce some fundamental actions and terms that are central to writing good code.

Commenting

Leaving comments helps future you understand your thought process. As a general rule, keep it short and meaningful. Code should be commented where reading the code may cause confusion.

// Example single line comment.  Preferred style.

/* 
    Muli-line comment
    Use for commenting out large blocks of code temporarily.
    Don't use long-term.  Makes reverting comments difficult.
*/

Importing

There are numerous libraries that provide added functionality to your code. This is a clean way to import other's code into your own, effectively extending the possibilities of your code with minimal effort.

// Example iOS imports
import UIKit // Imports iOS UIKit library for various controls such as UIButton, UILabel, etc.
import Foundation // Imports foundation objects, typically prefaced with "NS" such as NSString, NSData, etc.

Storing Data

Storing data is complicated. Data can be structured in a variety of ways. Data can be sent and received in a variety of ways. Swift provides a variety of buckets you can use for storing data. The comments in the below code delineate various types of in-memory data storage. It's important to start thinking about how you will store and manipulate data.

// Storing one piece of data
let myName = "Jeff"

////
// Storing an array of data
let dangerousSnakes: [String] = ["Rattlesnake", "Cobra", "Python"]

////
// Storing a rectangle
struct Rect {
    var length:Int
    var width:Int
    var color:String
    var area:Int {
        get {
            return length * width
        }
    }
}

let rectangle = Rect(length: 10, width: 10)
let area = rectangle.area // 100

////
// Storing a complex object
class ComplexObject {
    var objectName:String
    var objectType:String
    var objectAge:Int
    var spouse:ComplexObject?

    init(name:String, type:String, age:Int) {
        self.objectName = name
        self.objectType = type
        self.objectAge = age
    }

    func becomeMarried(partner:ComplexObject) -> () {
        self.spouse = partner
    }
}

let me = ComplexObject(name: "Jeff", type: "Human", age: 34)
me.objectType

let wife = ComplexObject(name: "Katie", type: "Human", age: 32)
me.becomeMarried(wife)
wife.objectType

Scope

Scope of a variable refers to where a particular variable's binding is valid. For example, open Playgrounds and paste the following. You can trace the scope in the right-hand panel.

var outsideVariable = 1

class ExampleClass {

    var insideClassVariable = 2

    func exampleFunction(functionVariable:Int) -> (Int) {
        var insideFunctionVariable = 3 + functionVariable

        print(insideFunctionVariable)
        print(functionVariable)
        print(self.insideClassVariable)
        print(outsideVariable)

        return (insideFunctionVariable)
    }
}

let example = ExampleClass()
let responseFromFunction = example.exampleFunction(4)

print(outsideVariable)

And the output from the right-pane should be similar to this:

References

Swift Programming Guide - https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/

Scope (Programming) - http://en.wikipedia.org/wiki/Scope_(computer_science)

The Swift Programming Language - A Swift Tour https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GuidedTour.html