How to check if a variable is nil

I have a variable

var a: [AnyObject? -> Void] 

and I add data to it with the append method. Now I want to check if the variable is null or not. I tried to use [], but didn’t work, and also tried "", it doesn’t work either, can someone tell me what is the meaning of this variable and how to check if it is null.

+4
source share
6 answers

As far as I understand, var ais an array of functions that take an optional object of any type and return void. Thus, these functions have an IS optional parameter, but the array itself is not: it cannot be nil or declared [AnyObject? -> Void]?, no?

EDIT: , , a ( ?) - ? - if let

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

, , isEmpty Swift Array

+7

: Xcode 7.3 Swift 2.2

, , , , . var.

let str = "123"
var a = Int(str)        
if let a = a {
    print(a)
}

if let a = Int(str) {
    print(a)
}
+4

Swift - . , .

, :

let possibleNumber = "123"
let convertedNumber = possibleNumber.toInt()

if convertedNumber != nil {
    println("convertedNumber contains some integer value.")
}
// prints "convertedNumber contains some integer value."

, nil .

+2

Swift 3.0

if let imageURL = dictObj["list_image"] as? String {
   print(imageURL)
}
+1

, . let - Swift, , Optional value, - - .

var a:Int=0
if let b=a{
   println(a)
} else {
   println("Value - nil")
}

.isEmpty() "".

var str:String=""
if !str.isEmpty(){
   println(str)
}
0

, AVFoundation.

Type 'AVCaptureDeviceInput does not conform to protocol 'BooleanType', if (audioDeviceInput), Binary operator '!=' cannot be applied to operands of type 'AVCaptureDeviceInput' and 'nil'.

if (audioDeviceInput.isEqual(nil))

nil - , , , .

0

All Articles