Conflicting documentation for `[UIView initWithFrame:]`: nullable or nonnull?

Using CLANG_ANALYZER_NONNULL(i.e. -Xclang nullability), I got "Null returns from a function that should return a non-zero value":

enter image description here

Using the Xcode 7.3 and iOS 9.3 documentation, I checked initWithFrame:and returned nil:

description

But UIView.h encapsulates everything with NS_ASSUME_NONNULL_BEGIN, so we can interpret the following:

interface

as:

- (nonnull instancetype)initWithFrame:(CGRect)frame NS_DESIGNATED_INITIALIZER;

So the documentation explains this nullable, and the header file says nonnull. Who to trust?

Should I write:

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (!self) {
        // workaround for clang analyzer
        return (void * _Nonnull)nil;
    }
    // Initialization code
    return self;
}

Or:

- (nonnull instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    // Initialization code
    return self;
}

🔴🔴🔴

UPDATE

The Xcode documentation has been updated and now: initWithFrame: documentation

So there are more conflicts.

+6
2

UIView -initWithFrame , , UIView.

, Xcode 7.3 beta 4, . nil -init, -copy -mutableCopy, nonnull, .

+3

, , . UIKit ( CocoaTouch ) , : Swift .

, :

- (nonnull instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    // Initialization code
    return self;
}

nonnull , UIView , -, , .

nil [NSURL URLWithString:], NSURL , <<28 > , nonnull .

+1

Source: https://habr.com/ru/post/1629743/


All Articles