why do other properties need nullability type specifier

85 views Asked by At

I am getting the date when a photo was created using

@property(nonatomic, readonly, nullable) AVMetadataItem *creationDate;

in my .h file. Then I get the value of the AVMetadataItem of my video asset.

This works fine however when I add this to my .h file all the rest of my properties get a warning that they are missing a nullability type specifier, even though they don't need it when I don't have this property listed. How do I do this without having to add a nullablity specifier to every other property etc. in the .h file?

1

There are 1 answers

0
Rob Napier On BEST ANSWER

Once you add a nullability annotation, ObjC expects that you have audited the file. To mark regions of the file to be nonull by default, use the NS_ASSUME_NONNULL_BEGIN and NS_ASSUME_NONNULL_END macros.

From the Nullability and Objective-C blog post:

NS_ASSUME_NONNULL_BEGIN
@interface AAPLList : NSObject <NSCoding, NSCopying>
// ...
- (nullable AAPLListItem *)itemWithName:(NSString *)name;
- (NSInteger)indexOfItem:(AAPLListItem *)item;

@property (copy, nullable) NSString *name;
@property (copy, readonly) NSArray *allItems;
// ...
@end
NS_ASSUME_NONNULL_END

// --------------

self.list.name = nil;   // okay

AAPLListItem *matchingItem = [self.list itemWithName:nil];  // warning!

This will replace all the ! types in your Swift bridging with explicit non-optional types. You should audit the code to make sure this is correct. But for the common case, where most things are nonnull, the macros greatly simplify adoption.