How to Check If a Variable Is Nil

Checking if a variable is not nil and not zero in ruby


unless discount.nil? || discount == 0
# ...
end

How can I determine if a variable is 'undefined' or 'null'?

You can use the qualities of the abstract equality operator to do this:

if (variable == null){
// your code here.
}

Because null == undefined is true, the above code will catch both null and undefined.

Is there a simpler way to check if a variable is `nil` or an empty array?

This would work:

my_arr.to_a.empty?

Because:

nil.to_a #=> []

An array instance on the other hand just returns itself.

The same "trick" works for other classes:

nil.to_h.empty? #=> true
nil.to_s.empty? #=> true
nil.to_i.zero? #=> true

How to check for nil and compare if != nil on Hashes?

If you want to check if an object/value is nil you can do it like this:

object.nil?

in your case (the value of a hash is something like): your_hash[key].nil?, with that said if you want to do something if the value is not nil you can do:

if !my_hash[my_key].nil?
end

or the ruby way:

unless my_hash[my_key].nil?
end

Also if you want to know whether a hash contains or not a key you can do it as follow:

your_hash.keys.include? key_to_search

If you want to compare 2 strings: str1 == str2, if you want to know if str1 contains to str2 as substring then str1.include? str2

How to see if value of variable is Nil

Use this as NSString may contains white spaces i think best solution for validate NSString value.

NSCharacterSet *charSet = [NSCharacterSet whitespaceCharacterSet];
NSString *result = [self.foodName.text stringByTrimmingCharactersInSet:charSet];
if ([result isEqualToString:@""]) {
NSLog(@"No Value Found");
}

Swift: Testing optionals for nil

In Xcode Beta 5, they no longer let you do:

var xyz : NSString?

if xyz {
// Do something using `xyz`.
}

This produces an error:

does not conform to protocol 'BooleanType.Protocol'

You have to use one of these forms:

if xyz != nil {
// Do something using `xyz`.
}

if let xy = xyz {
// Do something using `xy`.
}

How to check, whether a Date is nil or not?

In your code, you cannot forcibly unwrap _signedDate always when accessing signedDate since it will throw a run time error if _signedDate is nil.

Either have a default value for signedDate when _signedDate is nil, or expose signedDate to be also of type Date?.

One can check if a date is nil by using:

func hasSigned() -> Bool {
if _signedDate == nil {
return false
}
return true
}

To provide a default date (if it makes sense):

private var _signedDate:Date? = nil
var signedDate:Date {
get {
return _signedDate ?? Date() // or some valid date
}
set (newDate) {
_signedDate = newDate
}
}

Best way to check if a Variable is nil?

It's usually the same... except when you check a function...

function mfi: TObject;
begin
Result := nil;
end;

procedure TForm1.btn1Click(Sender: TObject);
type
TMyFunction = function: TObject of object;
var
f: TMyFunction;
begin
f := mfi;

if Assigned(f) then
begin
ShowMessage('yes'); // TRUE
end
else
begin
ShowMessage('no');
end;

if f <> nil then
begin
ShowMessage('yes');
end
else
begin
ShowMessage('no'); // FALSE
end;
end;

With the second syntax, it will check the result of the function, not the function itself...

Checking if variable type is nil when using interface

Avoid checking for nil concrete value in interface because a nil value may be a valid implementation of the interface. Here's a somewhat contrived example of where nil is valid:

type exampleCacher struct { }

func (c *exampleCacher) Get(key interface{}) (value interface{}, ok bool) }
if c == nil {
return nil, false
}
...
}

A better fix to the problem is to ensure that the code only assigns valid values to r.Cache.

The code in the question always sets r.Cache to a non-nil value because the code assigns a concrete type to r.Cache. See the FAQ entry on nil error for an explanation.

Fix the problem by declaring cache as a Cacher.

var cache Cacher

As I mention in my comment above, another fix is:

if cache != nil { 
r.Cache = cache
}


Related Topics



Leave a reply



Submit