Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSError, Swift, and nullability

I'm writing a set of tools in Objective-C that will be used by Swift at some point so I'm using generics and nullability. What am I supposed to do in this situation?

- (NSArray<MyObj *> * __nullable)foo:(NSError **)error;

Currently I'm getting a warning: Pointer is missing a nullability type specifier... for both pointers! I'm almost certain that I'm NOT to supposed to do:

- (NSArray<MyObj *> * __nullable)foo:(NSError * __autoreleasing __nullable * __nullable)error;

Am I?

like image 222
Rob Sanders Avatar asked Oct 18 '15 13:10

Rob Sanders


1 Answers

The Swift blog entry Nullability and Objective-C states:

The particular type NSError ** is so often used to return errors via method parameters that it is always assumed to be a nullable pointer to a nullable NSError reference.

However, this remark is listed as an exception to the rules of "Audited Regions", and it seems that it applies only within an audited region:

NS_ASSUME_NONNULL_BEGIN

@interface MyClass : NSObject
- (NSArray<MyObj *> * _Nullable)foo:(NSError **)error;
@end

NS_ASSUME_NONNULL_END

Within an audited region, any simple pointer type will be assumed to be nonnull (with some exceptions as the above-mentioned for NSError).

Outside of an audited region, you actually have to write explicitly

- (NSArray<MyObj *> * _Nullable)foo:(NSError * _Nullable * _Nullable)error;

to avoid warnings about missing nullability type specifiers.

(_Nullable is the newer syntax used in Xcode 7 and replaces __nullable.)

like image 163
Martin R Avatar answered Nov 09 '22 22:11

Martin R