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,整個方法塊已經被標記爲返回非空值。)看起來像這些方法上的文檔應該列出它們爲非空。 –

相關問題