Pointer is missing a nullability type specifier

后端 未结 6 1800
情书的邮戳
情书的邮戳 2021-01-31 01:17

In Xcode 7 GM I started to get this warning:

Pointer is missing a nullability type specifier (_Nonnull, _Nullable, or _Null_unspecified)

6条回答
  •  走了就别回头了
    2021-01-31 02:10

    You can use the following macros around blocks of declarations (functions and variables) in objective c headers:

    NS_ASSUME_NONNULL_BEGIN 
    
    NS_ASSUME_NONNULL_END
    

    You need to then add nullable annotations for references that can be nil within that block. This applies to both function parameters and variable declarations.

    As in:

    @interface SMLBaseUserDetailsVC : UIViewController < UICollectionViewDelegate>
    NS_ASSUME_NONNULL_BEGIN
    
    @property (nonatomic, readonly) IBOutlet UIScrollView *detailsScrollView;
    @property (nonatomic, readonly) IBOutlet UICollectionView *photoCV;
    @property (nonatomic, weak, readonly) SMLUser *user;
    - (IBAction)flagUser:(id)sender;
    - (IBAction)closeAction:(nullable id)sender;
    - (void) prefetchPhotos;
    
    NS_ASSUME_NONNULL_END
    
    @end
    

    Edit* The why??? is because for an objective-c class to be interoperable with swift, you need to declare nullability so that the compiler knows to treat properties as swift optionals or not. Nullable objective c properties are known as optionals in swift and using these macros in conjunction with the nullable declarators for properties allows the compiler to treat them as optionals (Optionals are monads - an object that wraps up either the object or nil).

提交回复
热议问题