swift - passing integer Argument in a function -
func getdriverfromid(var id : int?) -> string { if id == nil { return "no driver assigned" } else { loaddriversrest() } return "test" } getdriverfromid(nil)
i getting error "getdriverfromid
argument list of type (nil)"
your code correct , works fine. said have couple of suggestions , have included (working) playground you.
given code first checks whether id nil, don't need else flow stop (or return) anyway - makes little more pleasing eye...
with in mind, function this:
func getdriverfromid(var id : int?) -> string { if id == nil { return "no driver assigned" } loaddriversrest() return "test" }
also, unless plan on changing "id" within code of function, don't need var before it. basically, function arguments non-mutable default can't change them, adding var enables change value of argument within code.
with of in mind, here playground showing working. hope helped...
import uikit func loaddriversrest() { // doesn't return } func getdriverfromid(id : int?) -> string { if id == nil { return "no driver assigned" } loaddriversrest() return "test" } println(getdriverfromid(nil)) // works println(getdriverfromid(15)) // works
Comments
Post a Comment