Vayn
Vayn

Reputation: 2717

NULL parameter in Swift

In Objective-C, we can declare a function like this:

- (void)getRect:(CGRect *)aRectRef bRect:(CGRect *)bRectRef
{
    if (aRectRef) *aRectRef = CGRectZero
    if (bRectRef) *bRectRef = CGRectZero
}

and pass NULL to the function:

CGRect rect;
[self getRect:NULL bRect:rect]

There isn't NULL in Swift. I can't use nil as inout param directly either:

func getRect(aRect aRectRef: inout CGRect?, bRect bRectRef: inout CGRect?) -> Void {
    ...
}
self.getRect(&nil, bRect: rect) // <- ERROR

I must define a variable with nil value and pass it to the function, even though I don't need the variable totally.

How to pass nil to the function?

UPDATE:

null / nil in swift language just explained nil in Swift.

Swift optional inout parameters and nil explained how to define a variable with nil value and pass it as inout parameter.

I want to know there is a way to pass nil directly like &nil to function or not.

Upvotes: 3

Views: 3474

Answers (2)

Martin R
Martin R

Reputation: 539685

Your Objective-C method has nullable pointers as parameters, in Swift 3 that would be an optional UnsafeMutablePointer:

func getRect(aRectRef: UnsafeMutablePointer<CGRect>?, bRectRef: UnsafeMutablePointer<CGRect>?) {
    if let aRectPtr = aRectRef {
        aRectPtr.pointee = CGRect(x: 1, y: 2, width: 3, height: 4)
    }
    if let bRectPtr = bRectRef {
        bRectPtr.pointee = CGRect(x: 5, y: 6, width: 7, height: 8)
    }
}

var rect = CGRect.zero
getRect(aRectRef: &rect, bRectRef: nil)
print(rect) // (1.0, 2.0, 3.0, 4.0)

So you can pass nil as an argument. What you can not do (in contrast to Objective-C) is to pass the address of an uninitialized variable, rect must be initialized here.

The same can be written more compactly as

func getRect(aRectRef: UnsafeMutablePointer<CGRect>?, bRectRef: UnsafeMutablePointer<CGRect>?) {

    aRectRef.map { $0.pointee = CGRect(x: 1, y: 2, width: 3, height: 4) }
    bRectRef.map { $0.pointee = CGRect(x: 5, y: 6, width: 7, height: 8) }
}

Upvotes: 2

mdang
mdang

Reputation: 155

Sorry, I am not allowed to add comment at this time, so I'll write this as an answer. In SWIFT, you defined the parameter as inout, you have to pass in a variable and not literal nil. You can do something like this,

func testGetRect()
    {
        var recta: CGRect? = nil
        var rectb: CGRect? = CGRect()

        self.getRect(aRect: &recta, bRect: &rectb)
    }

    func getRect(inout aRect aRectRef: CGRect?, inout bRect bRectRef: CGRect?) -> Void
    {
        if (aRectRef != nil)
        {
            aRectRef = CGRectZero
        }
        if (bRectRef != nil)
        {
            bRectRef = CGRectZero
        }

    }

Yes, you "must define a variable with nil and pass it to the function. I tried some casting to see if it work, but couldn't. The parameter passing with inout is like C++ parameter passing by reference, i.e foo(int &parama, int &paramb). You must pass a variables to foo. I don't believe SWIFT has parameter passing like the obj-c example that you have.

Upvotes: 0

Related Questions