Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>Return values from a nil receiver</h2> <p>When accessing properties or reading return values from a nil object, you will get their default value. This is usually 0 for any numeric return type. So getting the count of an array when it is nilled will yield 0. Other possible values from nil receivers are NO for BOOLs, and nil for object return types. Returned structures have all members initialized to zero.</p> <h2>Array counts are unsigned...</h2> <p>Now, you need to remember that an array count returns an NSUInteger. With this being unsigned, if you subtract from 0, you will underflow, and get a very large number.</p> <h2>Why does NSLog print <code>-1</code> for the first statement then?</h2> <p>It's because you have used <code>@"%ld"</code>, which specifies a long <strong>signed</strong> integer. As such, the value is interpreted as signed, and this results in -1. The type of the variable actually states it's an unsigned long, whose format specifier should be <code>@"lu"</code>. When using this, it results in 18446744073709551615 for me (could vary for you, depending on platform).</p> <h2>How does this affect the second NSLog statement?</h2> <p>Taking into account what's going on in the first statement, the second statement may now make more sense. You may have thought it was comparing <code>0 &lt; -1</code>, which results in NO, and shouldn't produce a result of 1. What's actually being compared, is <code>0 &lt; 18446744073709551615</code>, which results in YES. This is why you're getting a result of 1.</p> <p>It all boils down to using the incorrect format identifier in NSLog, which caused confusion on how to interpret the value.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload