我们可以在Objective-C中创建nullable/nonnull属性吗?
问题描述:
如果可以的话,我们可以在Objective-C中创建可为空/不为空的属性吗?
Can we create nullable/nonnull property in objective-c if yes then how?
答
您可以使用_Nullable
和_Nonnull
限定词
@property (copy, nullable) NSString *name;
@property (copy, nonnull) NSArray *allItems;
nonnull:指示指针应该/永远不会为nil.用nonnull注释的指针作为其非可选基值(即NSData)导入到Swift中.
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?).