NSError, Swift, and nullability

后端 未结 1 1699
甜味超标
甜味超标 2021-02-02 02:50

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?



        
1条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-02 03:32

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

    0 讨论(0)
提交回复
热议问题