Advertisement
Guest User

Untitled

a guest
Nov 28th, 2014
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.49 KB | None | 0 0
  1. class MyObj {
  2.  
  3. var foobar:String
  4.  
  5. init(foo: String?="hello") {
  6. self.foobar = foo!
  7. }
  8. }
  9.  
  10. let myObj = MyObj() // standard use of default values - don't supply a value at all
  11.  
  12. println(myObj.foobar) // prints "hello" as expected when parameter value is not supplied
  13.  
  14. var jimbob: String? // defaults to nil
  15.  
  16. ...
  17.  
  18. // supply a value, but it is nil
  19. let myObj2 = MyObj(foo: jimbob) // <<< this crashes with EXC_BAD_INSTRUCTION due to forced unwrap of nil value
  20.  
  21. println(myObj2.foobar)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement