Can we create nullable/nonnull property in objective-c if yes then how?
Annotate Nullability of Individual Declarations You can use nullability annotations in your Objective-C code to designate whether a parameter type, property type, or return type is nullable.
There are two annotations we can apply to our Objective-C code to improve this situation: Marking types as nonnull imports them as non-optional in Swift. Marking types as nullable imports them as optional in Swift.
The goal of the @property directive is to configure how an object can be exposed. If you intend to use a variable inside the class and do not need to expose it to outside classes, then you do not need to define a property for it. Properties are basically the accessor methods.
You can use _Nullable
and _Nonnull
qualifiers
@property (copy, nullable) NSString *name;
@property (copy, nonnull) NSArray *allItems;
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: Indicates that the pointer can be nil in general practice. Imported into Swift as an optional value (NSURL?).
You can use _Nullable
and _Nonnull
qualifiers as you see fit. (Put them in the same place as you'd place a const
qualifier).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With