Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we create nullable/nonnull property in objective-c?

Can we create nullable/nonnull property in objective-c if yes then how?

like image 311
Mayank Avatar asked Oct 20 '15 09:10

Mayank


People also ask

What is nullable in Objective-C?

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.

How many annotations are there in Objective-C?

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.

What are Objective-C properties?

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.


2 Answers

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

like image 130
Rahul Sharma Avatar answered Sep 28 '22 05:09

Rahul Sharma


You can use _Nullable and _Nonnull qualifiers as you see fit. (Put them in the same place as you'd place a const qualifier).

like image 34
Bathsheba Avatar answered Sep 28 '22 03:09

Bathsheba