Xcode中与null相关的属性属性有什么作用? [英] What do the null-related property attributes in Xcode do?

查看:84
本文介绍了Xcode中与null相关的属性属性有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Xcode 6.3中,我注意到了一些属性属性,即:

With Xcode 6.3 I noticed some property attributes, namely:

  • nonnull
  • null_resettable
  • nullable
  • nonnull
  • null_resettable
  • nullable

有人可以解释一下应用后会做什么吗?

Could someone explain what they do when applied?

推荐答案

Apple添加了两个新的类型注释:__nullable和__nonnull. __nullable指针可能具有NULL或nil值,而__nonnull则不应该具有NULL或nil值.

Apple has added two new type annotations: __nullable and __nonnull. __nullable pointer may have a NULL or nil value, while a __nonnull one should not have.

您应该很快就知道可以使用可选选项(?),但在Objective-C中则不能.这些属性使您可以创建Objective-C代码,在违反规则时可以通过迅速且编译器警告您来更好地理解它们,例如:

As you should know in swift you can use optionals (?) but in Objective-C you cannot. Those attributes let you create Objective-C code which is more understandable by swift and complier warn you when you break the rule, for example:

@property (copy, nullable) NSString *name;
@property (copy, nonnull) NSArray *allItems;

这将迅速翻译"为:

var name: String?
var allItems: [AnyObject]!

这取自NSHipster:

This is taken from NSHipster:

nonnull:指示指针应该/永远不会为nil.指针 带nonnull的注释作为非可选内容导入到Swift中 基本值(即NSData).

nonnull: Indicates that the pointer should/will never be nil. Pointers annotated with nonnull are imported into Swift as their non-optional base value (i.e., NSData).

nullable:表示通常情况下指针可以为nil. 作为可选值(NSURL?)导入到Swift中.

nullable: Indicates that the pointer can be nil in general practice. Imported into Swift as an optional value (NSURL?).

null_unspecified:继续当前的功能 导入到Swift中作为隐式展开的可选内容,理想情况下是 只能在此注释过程中使用.

null_unspecified: Continues the current functionality of importing into Swift as an implicitly unwrapped optional, ideally to be used during this annotation process only.

null_resettable: 表示虽然属性将始终具有值,但它可以是 通过分配nil重置.具有非零默认值的属性可以是 这样注释,就像tintColor一样.导入到Swift作为一个 (相对安全)隐式展开的可选.相应地记录!

null_resettable: Indicates that while a property will always have a value, it can be reset by assigning nil. Properties with a non-nil default value can be annotated this way, like tintColor. Imported into Swift as a (relatively safe) implicitly unwrapped optional. Document accordingly!

这篇关于Xcode中与null相关的属性属性有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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