8

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

Mayank
  • 643
  • 8
  • 19
  • 1
    Possible duplicate of [What is nonnull in objective C?](http://stackoverflow.com/questions/31002033/what-is-nonnull-in-objective-c) – Anbu.Karthik Oct 20 '15 at 09:33
  • Possible duplicate of [What do the null-related property attributes in XCode do?](http://stackoverflow.com/questions/29532640/what-do-the-null-related-property-attributes-in-xcode-do) – Arnaud Christ Oct 20 '15 at 09:44

2 Answers2

13

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

zaph
  • 111,848
  • 21
  • 189
  • 228
Rahul Sharma
  • 153
  • 6
  • I think i don't have this "nullable/nonnull" headers installed, i get errors, why headers do i need to update ? – Osa Jan 19 '16 at 12:30
5

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

Bathsheba
  • 231,907
  • 34
  • 361
  • 483