I’m going to pass nil, you’re going to give me a warning, and when I see a warning, I’m going to eliminate you

Empty warning

Null passed to a callee that requires a non-null argument

When I pass a parameter to a method, I’m given this warning, which translates to null values being passed to callers that require non-null arguments. If you send me an empty parameter, I’ll send you a warning.

Think about it this way, feel swift is not much better, for this aspect of security also do better.

Problem solving

Since I want to pass the empty past, I’ll just allow it to be empty in the parameters.

+ (void)didClicklink:(NSString *)link fromVC:(nullable UIViewController *)vc;
Copy the code

In the parameters, just add nullable.

So why is there a warning? Let’s go down.

expand

The problem is solved, so now let’s expand the knowledge of empty and non-empty.

nullable

Nullable, but empty. We can define methods or properties using nullable to allow null arguments.

@property (nonatomic, copy, nonnull) NSString *name;

- (void)test:(nullable NString *)string;
Copy the code

nonnull

Nonnull, cannot be null. You can also define methods or properties.

@property (nonatomic, copy, nonnull) NSString *name;

- (void)test:(nonnull NString *)string;
Copy the code

null_resettable

Null_resettable, set method can be null, get method cannot be null.

@property (nonatomic, copy, null_resettable) NSString *name; - (NSString *)name {if (_name == nil) {return @" NSString "; } return _name; }Copy the code

null_unspecified

Null_unspecified. The value is unspecified.

@property (nonatomic, copy, null_unspecified) NSString *name;
Copy the code

NS_ASSUME_NONNULL

When we create a New File, we’ll see that it’s wrapped by NS_ASSUME_NONNULL_BEGIN and NS_ASSUME_NONNULL_END.

#ifndef NS_ASSUME_NONNULL_BEGIN
#define NS_ASSUME_NONNULL_BEGIN _Pragma("clang assume_nonnull begin")
#endif
#ifndef NS_ASSUME_NONNULL_END
#define NS_ASSUME_NONNULL_END   _Pragma("clang assume_nonnull end")
#endif
Copy the code

The default value is nonNULL. When you compile, you get a warning if it is empty.

That’s why we set the parameter to nullable and don’t get a warning.