markdowns

IOS Development

A quick guide through Swift and IOS programming concepts.

Swift Swift


What is it?

Swift is a general-purpose, multi-paradigm, compiled programming language developed by Apple Inc. Swift was developed as a replacement for Apple’s earlier programming language Objective-C.

Syntax and Semantics

A statement is a command to perform an action. In swift we do not need to end a statement with a semi-colon => ;.

Commenting in Swift:

Syntax

Let’s assume a class called MyClass.
In Swift we name the file the same as the class.

Keywords
This are words reserved with a specific meaning to the compiler and cannot be used for other purposes.

[Keywords](https://docs.swift.org/swift-book/ReferenceManual/LexicalStructure.html)


Data Types

Swift is a strongly typed language, meaning that all data consists of a value and a type.

Types of Data

Variables

A variable refers to a memory location whose contents can be changed during program execution.

All variables must have the following characteristics:

Swift has type inference meaning you can either put the type of the variable or not, the compiler will do it for you in case you don’t put it. (Only on default values)

Identifiers

In swift we have two types of data, Constants and Variables.

We declare variables the following way: identifier variableName

Examples:


For loops


Basic For loop

for i in 0..<10{
  print(i)
}

For loop in array

for element in arr{
  print(element)
}

For loop in dictionary

let usersAndModules = ["phil": COMP228", "boris": "COMP109", "terry": "COMP329", "valli": "COMP318", “stuart" : "COMP39X"]

for (user, module) in usersAndModules {
  print(user, module)
}


Functions

Functions are named code blocks. Functions can have arguments and return values and correspond to procedures or methods in other languages.

// Declaring a function
func methodName(parameters: Type) -> ReturnType {
    ...
}

Example:

func sum(a: Int, b: Int){
    return a + b;
}

Classes and Structures

Classes

Instance of a class are reference types

class SomeClass{
  init(){  }
}

Structures

Instance of a class are value types

struct SomeStructure{
  init(){  }

}

Creating an instance of a class or struct:

let classes = Class()
let structures = Structure()

Some rules

Classes

Enum

An object type whose instances represent distinct predefined alternative values

enum StarWarsRobot{
  case r2_d2 = "R2-D2"
  case c_3po
  case bb_8
  case k_2so
}

let type = StarWarsRobot.r2_d2

func handleRobot(_ : StarWarsRobot) {}

handleRobot((.bb_8))