默认情况下 Objective-C 函数结果是非空的吗?
Are Objective-C function results nonnull by default?
在 实现一个 UITextInput
委托方法时,发布者说当他们 运行 对其代码进行静态分析时,他们在这个函数上遇到错误:
- (NSArray *)selectionRectsForRange:(UITextRange *)range
{
return nil;
}
错误是"nil returned from a method that is expected to return a non-null value."
函数声明的结果没有可空性说明符。默认情况下函数结果是否非空? (这些天我主要在 Swift 工作,并不了解 Objective-C 的最新变化。)
他们不是。
声明该方法的 header 用 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
所以这个方法实际上被标记为具有 non-null return 值。
在 UITextInput
委托方法时,发布者说当他们 运行 对其代码进行静态分析时,他们在这个函数上遇到错误:
- (NSArray *)selectionRectsForRange:(UITextRange *)range
{
return nil;
}
错误是"nil returned from a method that is expected to return a non-null value."
函数声明的结果没有可空性说明符。默认情况下函数结果是否非空? (这些天我主要在 Swift 工作,并不了解 Objective-C 的最新变化。)
他们不是。
声明该方法的 header 用 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
所以这个方法实际上被标记为具有 non-null return 值。