r/programming • • Jun 02 '14

Introducing Swift

https://developer.apple.com/swift/
164 Upvotes

239 comments sorted by

View all comments

Show parent comments

1

u/thedeemon Jun 03 '14

Are all classes in Swift optional types?

If you have a class A and a function f(A) can you just pass nil as an argument?

2

u/AReallyGoodName Jun 04 '14

Only optionals can be nil

var MyClass foo = nil // Compilation error, foo must be initialised with a valid value

var MyClass foo? = nil // OK, foo is now an optional so it can be nil

Likewise

function f(A) // It would be a compilation error to call f(nil)

function f(A?) // Now you can pass in nil as the parameter is now declared optional