1. Unavailable_attribute __attribute__ ((unavailable))
-(instancetype) init unavailable_attribute;
Tells the compiler that the method is not available and prompts the compiler for an error if it is forcibly invoked. For example, when a class is constructed, it does not want to initialize it directly through Init, and the Init method can be marked as unavailable by a specific initialization method (such as a singleton).
The effect is as follows:
The call will have a warning
2. Ns_assume_nonnull_begin, ns_assume_nonnull_end
If you need each attribute or each method to specify nonnull and nullable, it is a very tedious matter. In order to relieve our workload, Apple specifically provided two macros: Ns_assume_nonnull_begin and Ns_assume_nonnull_end. In the code between these two macros, all simple pointer objects are assumed to be nonnull, so we just need to specify those nullable pointers
- nonnull function: cannot be empty
How to use
Ns_assume_nonnull_begin @interface Testnullabilityclass () * items; -(ID) Itemwithname: (Nullable NSString *) name; @end Ns_assume_nonnull_end
In the preceding code, the Items property is nonnull by default, Itemwithname: The return value of the method is also nonnull, and the parameter is specified as nullable.
However, for security reasons, Apple has also set several rules:
- The nullability attribute of a type defined by a typedef is typically dependent on the context, even in audited regions, and cannot be assumed to be nonnull.
- A complex pointer type (such as ID *) must be shown to specify whether it is nonnull or nullable. For example, specify a nonnull pointer to a nullable object that can use __nullable ID * __nonnull.
- The nserror that we often use is usually assumed to be a nullable pointer to the nullable Nserror object.
3. Ns_designated_initializer
The OBJECTIVE-C is primarily NS_DESIGNATED_INITIALIZER implemented by macros to implement the specified constructor. The reason here is to use this macro is to tell the caller to initialize (construct) The class object using this method.
#define Ns_designated_initializer __attribute__ ((Objc_designated_initializer))- (instancetype) init; -(Instancetype) Initwithname: (NSString *) name Ns_designated_initializer;
/*Init is the convenience initializer method that simply calls Initwithname: (specifies the initializer) and sets a default value. Initwithname: Perform a full initialization and invoke the parent class's Init method. There are several rules for specifying the initializer: 1. Specifies that the initializer method must call the specified initializer method of the parent class, or call Super init when the parent class is nsobject. The 2.convenience initializer method must call other initialization methods, knowing that the last point to the specified initializer method. 3. A class if you specify an initializer method, you must implement the specified initializer method for all the parent classes. */-(instancetype) init {return[Self Initwithname:@"Unknown"];}-(Instancetype) Initwithname: (NSString *) name { Self=[Super Init]; if(self) {_name=[name copy]; } returnSelf ;}
iOS you must understand the use of system-defined macros