Swift : Useless default value for Optional? -


i'm creating function :

func foo(bar: uint? = 0) {   let dosomething = someotherfunc(bar!) } 

if i'm passing foo() nil value, i'm expecting default value of 0 used instead while unwrapping it, rather i'm getting usual error unexpectedly found nil while unwrapping optional value

where wrong here ?

the default value = 0 used if don't provide argument optional parameter:

func foo(bar: uint? = 0) {     println(bar) }  foo(bar: nil) // nil foo(bar: 1)   // optional(1) foo()         // optional(0), default value used 

if intention replace passed nil value 0 can use nil-coalescing operator ??:

func foo(bar: uint?) {     println(bar ?? 0) }  foo(nil) // 0 foo(1)   // 1 

Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

node.js - How to abort query on demand using Neo4j drivers -