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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With