NSError,Swift和可空性 [英] NSError, Swift, and nullability

查看:124
本文介绍了NSError,Swift和可空性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Objective-C中编写一些工具,将在某些时候使用Swift,因此我使用泛型和可空性。在这种情况下我应该做什么?

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

目前我正在收到警告: 指针缺少一个可空性类型说明符... 两个指针!我几乎肯定我不应该这样做:

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

我是吗?

解决方案

Swift博客条目可空性和目标C 状态:


特定类型 NSError ** 经常用于返回错误通过
方法参数,它始终被认为是一个可空的指针
到一个可空的 NSError 引用。


< blockquote>

但是,这句话被列为审计地区的规则
的一个例外,似乎它只适用于
审计区域:

  NS_ASSUME_NONNULL_BEGIN 

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

NS_ASSUME_NONNULL_END

在被审计的区域内,任何简单的指针类型将被假定为 nonnull (除了 NSError 之前的一些例外) p>

在被审计区域外,您实际上必须明确写入

  (NSArray< MyObj *> * _Nullable)foo:(NSError * _Nullable * _Nullable)错误; 

以避免有关缺少可空性类型说明符的警告。



_Nullable 是Xcode 7中使用的较新语法,并替换 __可空的。)


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?

解决方案

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

这篇关于NSError,Swift和可空性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆