How to Check Object Is Nil or Not in Swift

How to check object is nil or not in swift?

If abc is an optional, then the usual way to do this would be to attempt to unwrap it in an if statement:

if let variableName = abc { // If casting, use, eg, if let var = abc as? NSString
// variableName will be abc, unwrapped
} else {
// abc is nil
}

However, to answer your actual question, your problem is that you're typing the variable such that it can never be optional.

Remember that in Swift, nil is a value which can only apply to optionals.

Since you've declared your variable as:

var abc: NSString ...

it is not optional, and cannot be nil.

Try declaring it as:

var abc: NSString? ...

or alternatively letting the compiler infer the type.

How to check if `Any(not Any?)` is nil or not in swift

Using if case:

You can use if case Optional<Any>.none = a to test if a is nil:

var b: Bool?
var a = b as Any
if case Optional<Any>.none = a {
print("nil")
} else {
print("not nil")
}
nil
b = true
a = b as Any
if case Optional<Any>.none = a {
print("nil")
} else {
print("not nil")
}
not nil

Using switch:

You can use the pattern test with switch as well:

var b: Bool?
var a = b as Any

switch a {
case Optional<Any>.none:
print("nil")
default:
print("not nil")
}
nil

check if any property in an object is nil - Swift 3

I would strongly recommend against this. State validation is something which should happen from inside a class. From inside the class, you should know better how to check validity.

class Vehicle {
var name: String?
var model: String?
var VIN: String?

func isReadyToAdvance() -> Bool {
return name != nil && model != nil && VIN != nil
}
}

let objCar = Vehicle()
objCar.name = "Volvo"

if objCar.isReadyToAdvance() {
// Go to other screen
}

If there are subclasses with different rules for isReadyToAdvance() they can override that method.


If isReadyToAdvance() doesn't make sense for the base class, then add it as an extension.

extension Vehicle {
func isReadyToAdvance() -> Bool {
return name != nil && model != nil && VIN != nil
}
}

@iPeter asked for something a bit more compact when there are lots of properties.

extension Vehicle {
func isReadyToAdvance() -> Bool {
// Add all the optional properties to optionals
let optionals: [Any?] = [name, model, VIN]
if (optionals.contains{ $0 == nil }) { return false }

// Any other checks

return true
}
}

Best way to check a nil object

You can use guard statement for this purpose

guard let _ = object else {
// Do something
return
}

Swift: How to check if an AnyObject is nil or not?

If it is a variable/constant of type AnyObject, it can't be nil by definition (only optional types can be nil). What can be nil is something of type AnyObject? (an optional), and you check just like this:

// (optionalVariable is of type AnyObject?)

if let actualVariable = optionalVariable {
// it is non-nil, and its value is stored in actualVariable
// (type: AnyObject, non-optional).
// use it.
}
else{
// it is nil
}

How to check if an object is nil in Swift?

When an object is of a non-optional type, it cannot be nil, so the compiler does not let you perform a check at all.

When you use as operator with no question mark, you tell swift that he conversion must succeed, in which case you will never see nil as the result. If you are not sure if the conversion is going to succeed or not, use conversion to an optional type. You can combine it with an implicit nil check like this:

if let buttonSpringAnimation = self.pop_animationForKey(animationKey) as? POPSpringAnimation {
... // This block of code will be entered only when the conversion is successful
}

Check if TWO objects are nil in Swift, ios xcode


class SomeObject {

}

func returnWhatIsNotNil(objA: SomeObject?, objB: SomeObject?) -> SomeObject? {
if let objA = objA where objB == nil {
return objA
}
if let objB = objB where objA == nil {
return objB
}
return nil
}

Swift - How to check if var is nil?

first and last methods of array return optional value, that contains nil or element of array. Going forward, element of array can be nil by itself. Let me show easy example:

let dataArray: [String?] = ["a", nil]
let lastElement = dataArray.last

You think that lastElement have type String?, but it have type String?? instead. When you check dataArray.last for nil, you only confirm that array is not empty. I offer you to use optional binding instead:

if let lastElement = dataArray.last, _ = lastElement{         
print("dataArray.last != nil , last = " + String(dataArray.last));
}else{
print("dataArray is empty OR last element of dataArray is nil");
}


Related Topics



Leave a reply



Submit