3

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.)

jscs
  • 63,694
  • 13
  • 151
  • 195
Duncan C
  • 128,072
  • 22
  • 173
  • 272

1 Answers1

3

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.

jscs
  • 63,694
  • 13
  • 151
  • 195
  • Being pedantic but the method is not explicitly marked as having a non-null return value. That would require the use of `- (NSArray * _Nonnull)selection...`. The use of `NS_ASSUME_NONNULL_BEGIN` *implicitly* marks all of the methods until the `NS_ASSUME_NONNULL_END`. – rmaddy Aug 29 '17 at 00:23
  • Cool. Thanks. (And rmaddy, the whole block of methods has ben marked as returning non-null values.) Seems like the docs on those methods should list them as nonnull however. – Duncan C Aug 29 '17 at 01:03