Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a variable is nil

Tags:

swift

I have a variable

var a: [AnyObject? -> Void] 

and I am adding data in to it by append method. Now I want to check if the variable is nil or not. I tried using [] but not working and also tried "", this also not working, can anyone tell what is the meaning of this variable and how to check if it is nil.

like image 735
user2413621 Avatar asked Feb 04 '15 09:02

user2413621


1 Answers

As far as I understand, var a is an Array of functions that take an optional Object of any type, and return void. So these functions's parameter IS optional, but the Array itself isn't : it cannot be nil, or it would be declared [AnyObject? -> Void]? , no?

EDIT : if, nevertheless, you declared this a as an optional (but WHY would you do that ?) - adding a ? - you check an optional existence with if let :

if let b = a {
// a not nil, do some stuff
} else {
// a is null
}

If you just want to check if the array is empty, use isEmpty method from Swift Array

like image 86
Vinzzz Avatar answered Sep 30 '22 16:09

Vinzzz