Advanced Swift WWDC 2014

Taking control of syntax

The Thing Class

class Thing {

init(location: Thing?, name String, longDescription:String){}

weak var location: Thing?

var name: String

var longDescription: String


Ordinary Things

let wallWestOfHouse = Thing(location: westOfHouse,…)

let pathWestOfHouse = Thing(location: westOfHouse,…)

Changing Argument Names

class Thing {

init(location location: Thing?, name name String, longDescription longDescription:String){}


Making Something anonymous

for (key, _) in dict {    // ignore this value

println(key)    // since I just wanna key


Removing argument Names

class Thing {

init(_ location: Thing?, _ name String, _ longDescription:String){}   // call position name


Special Objects

Pulling Objects

// func performPull(object: Thing) {

if /* object is pullable*/ { /*pull it*/ } else { /*complain*/ }

}    // the parser will call this

Simple Protocols

protocol Pullable {

func pull()


adopting a protocol

class broads: Thing, Pullable {

func pull() {}


using a protocol value

func performPull (object:Thing){

if let pullableObject = object as Pullable {



/* complain */

println(“you arent sure how to pull a \(”)

// String interpolation 

how abt written object instead of


// Printable 

protocol Printable {

var description: String{/*get*/ return name}


Decorating a printed String

extension Thing {

vara nameWithArticle: String {

return “a ” + name



println(“you arent sure how to pull \(an ~ object)”)

overloading an operator

func ~ (decorator: ???, object: Thing)->String {



Adding a new operator

operator infix ~{}

func ~ () -> String{}

func an(obejct:Thing)-> String{

return object.nameWithArticle




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s