How do I write things in Swift?



Classes

class className {
   func one(){...}
   ...
}




Protocols

protocol protocolName {
   var one: type
   ...
}




Enums

enum enumName {
   case memberValue, anotherMemberValue
   ...
}




Structs

struct structName {
   var one: type
   ...
}




Mutating Properties of a Structure

struct structName {
   mutating func functionName {
      var one: type
      ...
   }
}




Functions

func functionName(parameters) -> returnType {
   var one: type
   ...
}




Class-Level Functions

class func functionName(parameters) -> returnType {
   var one: type
   ...
}




Extensions (Categories)

extension classToExtend: optionalProtocol {
   var one: type
   ...
}




In-out parameters

func functionName(inout parameterName: parameterType) {
   var one: type
   ...
}




Subscripting

struct structToSubscript {
   let constantToSubscript = value

   subscript( parameterName: parameterType ) -> returnType {
      var one: type
     ...
   }
}




Closures

Check out fuckingclosuresyntax.com for a more exhaustive list.


{ (parameters) -> returnType in statements }



{ (parameters) -> returnType in
   var one: type
   ...
   return result
}




Generics

func genericName<T>(parameterName: T) -> returnType {
   var one: type
   ...
}




Type-Casting

expression as type
expression as? type




Runtime Type-Checking

expression is type




String interpolation

println("Swift makes me feel \(object) inside")




@zzackkly