I am getting an array with null value. Please check the structure of my array below:
(
\"< null>\"
)
When I\'m trying to access
In Swift (or bridging from Objective-C), it is possible to have NSNull and nil in an array of optionals. NSArrays can only contain objects and will never have nil, but may have NSNull. A Swift array of Any? types may contain nil, however.
let myArray: [Any?] = [nil, NSNull()] // [nil, {{NSObject}}], or [nil, ]
To check against NSNull, use is to check an object's type. This process is the same for Swift arrays and NSArray objects:
for obj in myArray {
if obj is NSNull {
// object is of type NSNull
} else {
// object is not of type NSNull
}
}
You can also use an if let or guard to check if your object can be casted to NSNull:
guard let _ = obj as? NSNull else {
// obj is not NSNull
continue;
}
or
if let _ = obj as? NSNull {
// obj is NSNull
}