gpt4 book ai didi

objective-c - Xcode中null相关的property属性有什么作用?

转载 作者:太空狗 更新时间:2023-10-30 03:26:16 31 4
gpt4 key购买 nike

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

  • 非空
  • null_resettable
  • 可为空

有人可以解释一下他们在申请时做了什么吗?

最佳答案

Apple 添加了两个新的类型注释:

  • __nullable 指针可能有一个nil 值,
  • 虽然 __nonnull 不能有 nil

正如你应该知道的,在 Swift 中你可以使用 Ottionals,但在 Objective-C 中你不能。这些属性让您可以创建 Swift 更容易理解的 Objective-C 代码,编译器会在您违反规则时警告您,例如:

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

这将被迅速“翻译”为:

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

这取自NSHipster :

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

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

null_unspecified: Continues the current functionality ofimporting into Swift as an implicitly unwrapped optional, ideally tobe used during this annotation process only.

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

关于objective-c - Xcode中null相关的property属性有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29532640/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com