struct return of nil


Sean McBride
 

On Fri, 27 Apr 2018 11:57:15 +0700, Gerriet M. Denkmann said:

Is this guaranteed to work in the future?
See also:
<http://sealiesoftware.com/blog/archive/2012/2/29/objc_explain_return_value_of_message_to_nil.html>

Sean


Quincey Morris
 

On Apr 26, 2018, at 21:57 , Gerriet M. Denkmann <g@...> wrote:

Is this guaranteed to work in the future?

Yes:


“Working with nil

"Note: If you expect a return value from a message sent to nil, the return value will be nil for object return types, 0 for numeric types, and NO for BOOL types. Returned structures have all members initialized to zero."

This wasn’t true in the past, but at some point the clang compiler was changed to ensure a predictable result with a struct return type. (It isn’t true of the GCC compiler, AFAIK.)



Gerriet M. Denkmann
 

macOS 13.4; Xcode Version 9.3 (9E145).

This is (I believe) documented to work:

NSString *answer = …
if ( answer.length == 0 )
{
// works for answer = nil or answer = “” (empty string)
answer = @“No answer given”;
};

But what about this:

NSImage *image = …
if ( NSEqualSizes( image.size, NSZeroSize ) )
{
// works for image = nil or non-nil image with zero size
image = … some default image instead …
};

Is this guaranteed to work in the future?

Gerriet.