Objective-C函数结果默认是非空的吗?

时间:2017-08-28 23:28:39

标签: objective-c compiler-errors non-nullable objective-c-nullability

在实施UITextInput委托方法的this thread中,海报说当他们对代码运行静态分析时,他们会在此函数上出错:

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

错误是" nil从预期返回非空值的方法返回。"

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

1 个答案:

答案 0 :(得分: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

因此,此方法实际上已标记为具有非null返回值。