3

this thread上实现UITextInput委托方法,海报说,当他们在他们的代码运行静态分析,他们对这个功能得到一个错误:默认情况下,Objective-C函数的结果是否为非空?

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

的错误是“零从一个方法返回预计会返回一个非空值。“

函数声明对结果没有可为空的说明符。函数结果默认是非空的吗? (我目前主要在Swift工作,并且对Objective-C的最新更改不熟悉。)

回答

3

他们不是。

其中宣布方法用括号的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 

所以这种方法实际上已经被标记为具有非空返回值。

+0

迂腐,但该方法没有明确标记为具有非空返回值。这将需要使用' - (NSArray * _Nonnull)选择...'。使用'NS_ASSUME_NONNULL_BEGIN' *隐式地标记所有方法直到'NS_ASSUME_NONNULL_END'。 – rmaddy

+0

很酷。谢谢。 (而且rmaddy,整个方法块已经被标记为返回非空值。)看起来像这些方法上的文档应该列出它们为非空。 –

相关问题