Posts

Showing posts with the label Null

Check If CGRect Null In Getter

Answer : When you create an instance of your class, the _frame instance variable is automatically initialized, even before the init method is called. Since _frame is a C-struct ( CGRect ), its memory is cleared to all zeroes. This results in a CGRect with all zero values. CGRectNull is a special, non-zero CGRect . So your check using CGRectIsNull() will never be true. Using CGRectIsEmpty is a more proper check for this. Try - (CGRect)frame { if (CGRectIsEmpty(_frame)) _frame = CGRectMake(0,0,60,60); return _frame; } Here is an updated answer for Swift 4. You are able to simply use CGRect's isNull boolean property to check for CGRectNull now: let someRect = CGRect.null print(someRect.isNull) // true

Check If Object Exists In JavaScript

Answer : You can safely use the typeof operator on undefined variables. If it has been assigned any value, including null, typeof will return something other than undefined. typeof always returns a string. Therefore if (typeof maybeObject != "undefined") { alert("GOT THERE"); } There are a lot of half-truths here, so I thought I make some things clearer. Actually you can't accurately tell if a variable exists (unless you want to wrap every second line into a try-catch block). The reason is Javascript has this notorious value of undefined which strikingly doesn't mean that the variable is not defined, or that it doesn't exist undefined !== not defined var a; alert(typeof a); // undefined (declared without a value) alert(typeof b); // undefined (not declared) So both a variable that exists and another one that doesn't can report you the undefined type. As for @Kevin's misconception, null == undefined . It is due to type c...

Avoid Division By Zero In PostgreSQL

Answer : You can use NULLIF function e.g. something/NULLIF(column_name,0) If the value of column_name is 0 - result of entire expression will be NULL Since count() never returns NULL (unlike other aggregate functions), you only have to catch the 0 case (which is the only problematic case anyway): CASE count(column_name) WHEN 0 THEN 1 ELSE count(column_name) END Quoting the manual about aggregate functions: It should be noted that except for count , these functions return a null value when no rows are selected. I realize this is an old question, but another solution would be to make use of the greatest function: greatest( count(column_name), 1 ) -- NULL and 0 are valid argument values Note: My preference would be to either return a NULL, as in Erwin and Yuriy's answer, or to solve this logically by detecting the value is 0 before the division operation, and returning 0 . Otherwise, the data may be misrepresented by using 1 .