- Hands-On Design Patterns with Swift
- Florent Vilmart Giordano Scalzo Sergio De Simone
- 137字
- 2021-07-02 14:45:14
Using nullable, nonnull, _Nullable, and _Nonnull
With NS_ASSUME_NON_NULL_BEGIN/_END, you may need to mark particular properties as nullable as all of the code enclosed will be translated without optionals.
These four keywords are used to mark individual arguments or properties—their usage is straightforward:
- nullable
- nonnull
- _Nullable
- _Nonnull
Using a nullable property will produce Optional, which you'll safely need to unpack in Swift. Using nonnull will produce non-optional properties or variables. nullable and nonnull keywords are exclusively used in the context of declaring @property on Objective-C interfaces.
_Nullable and _Nonnull keywords are used in all other cases:
- Declaring local variables
- Arguments to methods
- Return types of methods
- Declaring ivars
You can use the following table as a reference if you ever need to quickly look up the different possibilities and how they translate in both languages: