Data Types

String
Int
Bool
Double
Float
Character

Variables & Constants

// Declaring
var myVariable:DataType
let myConstant:DataType

//Inferred Data Type
var myDouble = 1.0

// Optionals
var optionalVar:Double? = nil

// Safely "unwrap" safely an optional.
if let myFinalDouble = optionalVar {
  /*
    The sole purpose of it is to test if an optional variable contains
    an actual value and bind the non-optional form to a temporary variable.
  */
}

/* Lazy variables:
   Delays the creation of an object or some other expensive process until it’s needed
*/
lazy var refreshControl: UIRefreshControl = {
  let rc = UIRefreshControl()
  rc.attributedTitle = NSAttributedString(string: "Fetching...")
        
  return rc
}()

Control Flow

// Simple If
if condition {

} else if condition {

} else {

}

// Swtich
switch value {
  case value:
  case value:
  default:
}

Loops

for variable in lower...upper {
  // ..
}

while condition {
  // ..
}

repeat {
  // ..
} while condition

Enums

enum PlayerType: Int {
  case rogue = 1
  case fighter = 2
  case cleric = 3
}

var type = PlayerType.rogue

Declaring Functions / Methods

func myFunction(arg param:DataType) -> ReturnType {

}

Classes

class MyClass : SuperClass, OptionalProtocol1, OptionalProtocol2 {
  var myProperty:String
  var myOptionalProperty:String? // More properties...

  // Only need override if subclassing
  override init() {
    myProperty = "Foo"
  }

  func sayHi(name: String) -> Void {
    print("Hello \(name)!!") 
  } 

  // more methods ...
}

// Creating/Using an Instance
var a = MyClass()
print(a.myProperty)

a.sayHi(name: "Daniel")

Arrays

// Declares an empty array of Strings
var myArray = [String]()

// Declares
var myArray:[String] = ["Hello", "Worldwide"]
myArray.append("World")

// Iterates
for word in myArray {
  print("The word is \(word)")
}

var world = myArray[2]

Dictionaries

// Declares
var myDict:[String: String] = ["name": "iPhone", "version": "7", "os": "iOS"]

// Iteration
for (myKey, myValue) in myDict {
  print("type: \(myKey), muppet: \(myValue)")
}

Extending

/*
 Extends the Collection type (array), to add a new function "decode"
 Only when is an array of JSON typee  
*/
extension Collection where Iterator.Element == JSON {
  // Uses generics to pickup the type "T" form the left side of the declaration
  func decode() throws -> [T] {
    return try map{try T(json: $0)}
  }
}

Bootstrap application w/o Storyboard

// AppDelegate.Swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions 
                launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

  window = UIWindow(frame: UIScreen.main.bounds)
        
  let homeController = MyViewController()
  let navigationControler = UINavigationController(rootViewController: homeController)
        
  window!.rootViewController = navigationControler
  window!.makeKeyAndVisible()
        
  return true
}
Hire me!

I'm currently open for new projects or join pretty much any project i may fit in, let me know!

Read about what I do, or contact me for details.

Categories