2

I'm implementing UIKeyInput, UITextInputTraits, and UITextInput and with that I'm required to implement:

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

However, in analyzing my project I get: "nil returned from a method that is expected to return a non-null value".

What would be the correct way to get rid of this? Should I do an abort?

Ser Pounce
  • 14,196
  • 18
  • 84
  • 169

2 Answers2

3

Actually if you look at the header source, the method has NS_ASSUME_NONNULL_BEGIN tag attached to it. So in short the selectionRectsForRange becomes a non null return method.

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

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

...

NS_ASSUME_NONNULL_BEGIN  // <--- HERE!
..
- (CGRect)firstRectForRange:(UITextRange *)range;
- (CGRect)caretRectForPosition:(UITextPosition *)position;
- (NSArray *)selectionRectsForRange:(UITextRange *)range NS_AVAILABLE_IOS(6_0); 

So you cannot return null or nil. Instead return an empty array like so:

- (NSArray *)selectionRectsForRange:(UITextRange *)range
{
    return @[];
}
GeneCode
  • 7,545
  • 8
  • 50
  • 85
2

Don't return nil. If you have no UITextSelectionRects to return, return an empty array.

matt
  • 515,959
  • 87
  • 875
  • 1,141