Are Objective-C function results nonnull by default?

539 views Asked by At

In this thread on implementing a UITextInput delegate method, the poster says that when they run static analysis on their code, they get an error on this function:

- (NSArray *)selectionRectsForRange:(UITextRange *)range
{
    return nil;
}

The error is "nil returned from a method that is expected to return a non-null value."

The function declaration does not have a nullability specifier on the result. Are function results non-null by default? (I work mostly in Swift these days, and am not expert on the latest changes to Objective-C.)

1

There are 1 answers

2
jscs On BEST ANSWER

They are not.

The header where that method is declared is bracketed with the "assume nonnull" macros:

//
//  UITextInput.h
//  UIKit
//
//  Copyright (c) 2009-2017 Apple Inc. All rights reserved.
//

#import <CoreGraphics/CoreGraphics.h>

#import <UIKit/UITextInputTraits.h>
#import <UIKit/UIResponder.h>

//===================================================================================================
// Responders that implement the UIKeyInput protocol will be driven by the system-provided keyboard,
// which will be made available whenever a conforming responder becomes first responder.

NS_ASSUME_NONNULL_BEGIN

// snip
// ...
// L150
/* Geometry used to provide, for example, a correction rect. */
- (CGRect)firstRectForRange:(UITextRange *)range;
- (CGRect)caretRectForPosition:(UITextPosition *)position;
- (NSArray *)selectionRectsForRange:(UITextRange *)range NS_AVAILABLE_IOS(6_0);       // Returns an array of UITextSelectionRects

So this method has in fact been marked as having a non-null return value.