Pointer missing nullability type specifier

In Xcode 7 GM, I started getting this warning:

The pointer does not have a nullability type specifier (_Nonnull, _Nullable or _Null_unspecified)

In the next function declaration (NSUserDefaults extension)

- (void)setObject:(nullable id)value forKey:(NSString *)defaultName objectChanged:(void(^)(NSUserDefaults *userDefaults, id value))changeHandler objectRamains:(void(^)(NSUserDefaults *userDefaults, id value))remainHandler; 

Why is this warning displayed and how to fix it?

+59
ios objective-c objective-c-nullability
Sep 12 '15 at 13:17
source share
6 answers

You must specify nullable also for handlers / blocks

 - (void)setObject:(nullable id)value forKey:(nonnull NSString *)defaultName objectChanged:(nullable void(^)(NSUserDefaults *userDefaults, id value))changeHandler objectRamains:(nullable void(^)(NSUserDefaults *userDefaults, id value))remainHandler; 

What for? This is because of Swift. Swift allows optional parameters (?), Which Objective-C does not have. This is done as a bridge between both of them so that the Swift compiler knows that these parameters are optional. "Nonnull" tells the Swift compiler that the argument is required. Nullable that's optional

For more information read: https://developer.apple.com/swift/blog/?id=25

+56
Sep 12 '15 at 17:00
source share

You can use the following macros around declaration blocks (functions and variables) in object headers c:

 NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_END 

Then you need to add annotations with a null value for the links, which can be zeros inside this block. This applies to both functional parameters and variable declarations.

How 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 * Why ??? because for an objective-c class that needs to be compatible with fast, you need to declare a nullability value so that the compiler knows that the properties are treated as fast or not. Incorrect object properties c are known as options in swift, and the use of these macros in combination with property declarations declared with zero declaration allows the compiler to treat them as options (options - monads - an object that wraps either an object or nil).

+54
Mar 22 '16 at 20:05
source share

The correct declaration of the working method accepted by the compiler:

 - (void)setObject:(nullable id)value forKey:(nonnull NSString *)defaultName objectChanged:(nullable void(^)(NSUserDefaults *_Nonnull userDefaults, id _Nullable value))changeHandler objectRamains:(nullable void(^)(NSUserDefaults *_Nonnull userDefaults, id _Nullable value))remainHandler; 
+14
Sep 14 '15 at 15:45
source share

I am posting this answer to say why add _Nonnull or nullable .

According to this blog: https://developer.apple.com/swift/blog/?id=25

One of Swift’s great features is that it transparently interacts with Objective-C code, both existing frameworks written in Objective-C and the code in your application. However, in Swift there is a strong distinction between optional and optional links, for example. NSView vs. NSView? , and Objective-C represents both of these two types as NSView * . Because the Swift compiler cannot determine whether a particular NSView * optional or not, the type is introduced into Swift as an implicitly expanded optional, NSView! .

All this for Swift.

+9
Aug 16 '16 at 3:14
source share

To disable this warning for your entire project

  1. Go to build settings

  2. Make sure you’re viewing the Everyone tab, not the General or Individual tab.

  3. Search for Other Warning Flags

  4. Add the following flag: -Wno-nullability-completeness .

  5. Now clean and collect (cmd + shift + k, then cmd + r)

  6. Exit xCode and reopen ( Do not skip this step! )

    --note, it may take a few seconds for the warnings to disappear, even after you finish building and restarting xCode, for me it will take about 15 seconds to fully update xCode

+3
Jul 20 '19 at 6:49
source share

Remove the macro below from your .h file and the warning disappears

 NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_END 
0
Sep 19 '19 at 7:24
source share



All Articles