NSError, Swift, and nullability

♀尐吖头ヾ 提交于 2019-12-31 10:03:49

问题


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?


回答1:


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.)



来源:https://stackoverflow.com/questions/33198597/nserror-swift-and-nullability

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!