Ser Pounce
Ser Pounce

Reputation: 14571

Nil returned from a method that is expected to return a non-null value

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?

Upvotes: 2

Views: 2937

Answers (2)

GeneCode
GeneCode

Reputation: 7588

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 @[];
}

Upvotes: 4

matt
matt

Reputation: 535999

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

Upvotes: 2

Related Questions