Skip to content

Instantly share code, notes, and snippets.

@goatandsheep
Last active August 29, 2015 14:24
Show Gist options
  • Save goatandsheep/402959268b5105988f25 to your computer and use it in GitHub Desktop.
Save goatandsheep/402959268b5105988f25 to your computer and use it in GitHub Desktop.
My notes on swift

iOS Development

  1. Set up apple developer account

Hints

  • If you want to restrict orientation, there is a secondary tag that is for iPad:
  • Swift supports polymorphism, overloading
  • API docs:
    • Success 200: response attributes, NOT parameters
  • beside the file lists, an A means "added" and M means modified relative to the last git commit
  • Sometimes for function calls, swift doesn't assume an order in your prototype, so you'll need to specify which variable is going to be which, e.g.
var a = 5
var b = 6
add(num1: a, num2: b)
	
func add(num1: Int, num2: Int){
  • Interface Builder (IB): the GUI where you can drag and drop views, change the colours, etc.
  • Easy way to get UI colors from hex
  • Instantiate an empty array: var someInts = [Int]()
  • NS: NeXTSTEP, the predecessor to iOS. NS classes are built-in Cocoa classes

Variables

You can set a value to a variable like this: var variable_name = expression (e.g. var a = 5.0). You can enforce a type like this: var variable_name: type = expression (e.g. var a:Int = 5.0).

You can declare a constant like: let constant_name: type = expression (e.g. let b:Float = 3.3e1)

Note: floating point numbers need to be enforced or they'll be considered double!

Storyboard

  • a view that defines the story that the user should follow while navigating through the app
  • connected with lines CTRL-Click
  • Each ViewController will have 3 options:
    • ViewController: not sure...
    • First Responder:
    • Exit: how to exit the view

Strings

  • to denote a variable within a string, instead of closing the quotation marks, do \(var)

Classes

When you import a class, you'll want to see how to use that class, so hover over the classname where you imported it, hold the command button, and click. All the functions will come up.

This is similar to when you want to use the functions of a built-in element, such as a UITableView. You import the UITableViewDelegate and UITableViewDataSource by adding it to the end of your class:

class viewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
  • classes can only inherit from one class at a time, i.e. no multi-inheritance. The exception is that you can inherit from infinitely many built-in classes, such as UIViewController
  • not copied when assigned to a variable or passed to a function
    • a.k.a. Reference Type
  • Import packages the same way you normally would (i.e. import package before the class definition

Structure

  • an object that has properties, methods, etc.
  • Doesn't do inheritance
  • Value is copied when assigned to another variable, instead of the second variable referring to the same instance
    • These types of objects are called value types
  • denoted by struct

Singleton: a variable that can only be defined once

Global Variables

Dependency Injection: instead of:

public SomeClass() {
    myObject = Factory.getObject();
}

it is:

public SomeClass (MyClass myObject) {
    this.myObject = myObject;
}

Unwind Segues

Segue: a fancy name for a transition from one view to another

Unwind segue: go back one/more steps in your navigation hierarchy

This can be done by taking a button and under Triggered Segues, drag a line from the action circle to the red Exit option at the top

Unwrap

If you try to print an attribute, it'll print stuff, like a pointer and ! unwraps the value like retrieving the value of a pointer

Functions in Functions

Swift allows you to have functions in functions, similar to javascript.

Versions

When you are collaborating with someone with a different version of XCode, you may get nan error called an Apple Mach-O Linker Errortry

Video

You'll probably have a player that integrates with the built-in class, AVPlayer. Sometimes when you run into network issues, your video buffer field will be empty, causing the video to automatically pause, without telling your player that it has paused

Key-Value Observing(KVO)

Cocoa Toach

The UI framework for iOS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment