Nil return 来自预期 return 非空值的方法
Nil returned from a method that is expected to return a non-null value
我正在实施 UIKeyInput
、UITextInputTraits
和 UITextInput
,因此我需要实施:
- (NSArray *)selectionRectsForRange:(UITextRange *)range
{
return nil;
}
但是,在分析我的项目时我得到:"nil returned from a method that is expected to return a non-null value"。
摆脱它的正确方法是什么?我应该中止吗?
不要returnnil
。如果你没有
UITextSelectionRects 到 return,return 一个空数组。
实际上,如果您查看 header 源代码,该方法附加了 NS_ASSUME_NONNULL_BEGIN
标记。所以简而言之,selectionRectsForRange
变成了一个非空的 return 方法。
//
// 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);
所以你不能 return null 或 nil。相反 return 一个像这样的空数组:
- (NSArray *)selectionRectsForRange:(UITextRange *)range
{
return @[];
}
我正在实施 UIKeyInput
、UITextInputTraits
和 UITextInput
,因此我需要实施:
- (NSArray *)selectionRectsForRange:(UITextRange *)range
{
return nil;
}
但是,在分析我的项目时我得到:"nil returned from a method that is expected to return a non-null value"。
摆脱它的正确方法是什么?我应该中止吗?
不要returnnil
。如果你没有
UITextSelectionRects 到 return,return 一个空数组。
实际上,如果您查看 header 源代码,该方法附加了 NS_ASSUME_NONNULL_BEGIN
标记。所以简而言之,selectionRectsForRange
变成了一个非空的 return 方法。
//
// 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);
所以你不能 return null 或 nil。相反 return 一个像这样的空数组:
- (NSArray *)selectionRectsForRange:(UITextRange *)range
{
return @[];
}