Swift optional inout parameters and nil

devios1

Is it possible to have an Optional inout parameter to a function in Swift? I am trying to do this:

func testFunc( inout optionalParam: MyClass? ) {
    if optionalParam {
        ...
    }
}

...but when I try to call it and pass nil, it is giving me a strange compile error:

Type 'inout MyClass?' does not conform to protocol 'NilLiteralConvertible'

I don't see why my class should have to conform to some special protocol when it's already declared as an optional.

Bryan Chen

It won't compile because the function expecting a reference but you passed nil. The problem have nothing to do with optional.

By declaring parameter with inout means that you will assign some value to it inside the function body. How can it assign value to nil?

You need to call it like

var a : MyClass? = nil
testFunc(&a) // value of a can be changed inside the function

If you know C++, this is C++ version of your code without optional

struct MyClass {};    
void testFunc(MyClass &p) {}
int main () { testFunc(nullptr); }

and you have this error message

main.cpp:6:6: note: candidate function not viable: no known conversion from 'nullptr_t' to 'MyClass &' for 1st argument

which is kind of equivalent to the on you got (but easier to understand)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Need help in using Swift inout with optional

Swift 2.0 'inout' function parameters and computed properties

Variable capture by closures in Swift and inout parameters

Swift 3 optional parameters

parameters with optional closures in swift

Can't pass nil to two optional parameters

Swift/iOS: How to use inout parameters in functions with AnyObject/Any or Pointers

Is there any difference between "mutating" function and "inout" parameters in Swift?

Is it possible to create an array of functions that take "inout" parameters in Swift?

Is it possible to point class instance variables to `inout` parameters in Swift?

Assigning value to inout parameters within closure in Swift 3

Swift Optional type: how .None == nil works

found nil while unwrapping optional in Swift

Swift nil Optional String = Empty String

Why is a Swift implicitly unwrapped optional `nil`?

Why is Swift nil-coalescing returning an Optional?

Swift optional assignment, deal with nil/empty string

Why assigning nil to an optional variable is true in Swift?

Best way to initialize non-optional var if optional is nil in Swift

When to use inout parameters?

IN, OUT, INOUT Parameters

Elixir: How to deal with optional / default parameters in functions and nil values?

Swift - Make the labels on constructor parameters optional?

Swift 3 - ! vs ? for optional function parameters

How to conveniently pass parameters only if they are not nil in swift?

inout parameter in Swift

Inout in swift and reference type

"in" vs "inout" in Swift

Unexpectedly found nil while unwrapping optional value (Swift, Parse)