Ios 从预期返回非null值的方法返回的Nil
我正在实现Ios 从预期返回非null值的方法返回的Nil,ios,objective-c,cocoa-touch,Ios,Objective C,Cocoa Touch,我正在实现uikeyinport、uiteImportRaits、和uiteImport,因此我需要实现: - (NSArray *)selectionRectsForRange:(UITextRange *)range { return nil; } 然而,在分析我的项目时,我得到:“nil从一个预期返回非空值的方法返回” 正确的方法是什么?我应该执行中止吗?不要返回nil。如果你没有 UITextSelectionRects要返回,请返回一个空数组。实际上,如果您查看头源,该方法会
uikeyinport
、uiteImportRaits
、和uiteImport
,因此我需要实现:
- (NSArray *)selectionRectsForRange:(UITextRange *)range
{
return nil;
}
然而,在分析我的项目时,我得到:“nil从一个预期返回非空值的方法返回”
正确的方法是什么?我应该执行中止吗?不要返回
nil
。如果你没有
UITextSelectionRects要返回,请返回一个空数组。实际上,如果您查看头源,该方法会附加
NS\u假设\u非空\u开始
标记。因此,简而言之,selectionRectsForRange
成为非空返回方法
//
// 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);
“我应该终止程序吗”?这是什么意思?拔掉你电脑的插头?@matt我换了它,我的意思是说,我应该做一个中止()。@SerPounce请检查我的答案。除了初始化一个长度为1的NSArray并返回它之外,还有更干净的方法吗?不是长度为1。空的。不要紧,我刚刚做了:return[[NSArray alloc]init]<代码>返回@[]代码>也许?
- (NSArray *)selectionRectsForRange:(UITextRange *)range
{
return @[];
}