6
+ (UIColor*) getColorWithHexa(NSString*)hexString;

enter image description here:

This is a method definition in my class. It's causing a warning. What is cause of similar warnings and how can these be resolved?

I am returning an UIColor object, while that question relates to blocks, which is given in comments.

So, it's helpful.

SolveSoul
  • 2,448
  • 4
  • 25
  • 34
Sam Shaikh
  • 1,596
  • 6
  • 29
  • 53
  • 2
    Don't post microscopic screenshots. Post code! Copy & paste is pretty easy. – vadian Feb 24 '16 at 11:28
  • @vadian both Questions are totally different. This questions relates to What is reason, its not a solution, or anything mentioned in your link. Here its a method, while that is complete separate block. – Chatar Veer Suthar Feb 24 '16 at 11:39
  • e.g. `+ (UIColor * _Nullable) getColorWithHexa(NSString * _Nonnull)hexString;`, but you can fit your class's entire interface between the `NS_ASSUME_NONNULL_BEGIN` and `NS_ASSUME_NONNULL_END` directives as well, which defines `_Nonnull` for every object automatically. – holex Feb 24 '16 at 12:16

2 Answers2

16

NS_ASSUME_NONNULL_BEGIN/END:

Annotating any pointer in an Objective-C header file causes the compiler to expect annotations for the entire file, bringing on a cascade of warnings. Given that most annotations will be nonnull, a new macro can help streamline the process of annotating existing classes. Simply mark the beginning and end of a section of your header with NS_ASSUME_NONNULL_BEGIN and ..._END, then mark the exceptions.

So,you have simply do.

NS_ASSUME_NONNULL_BEGIN
+ (UIColor*) getColorWithHexaCode:(NSString*)hexString;
NS_ASSUME_NONNULL_END

This is defined in

"NSObjCRuntime.h"

#define NS_ASSUME_NONNULL_BEGIN _Pragma("clang assume_nonnull begin")
#define NS_ASSUME_NONNULL_END _Pragma("clang assume_nonnull end")

Cœur
  • 37,241
  • 25
  • 195
  • 267
Chatar Veer Suthar
  • 15,541
  • 26
  • 90
  • 154
  • NS_ASSUME_NONNULL_BEGIN / END is really simple, until you specify a single non null parameter and then you'll be hounded by 'missing nullability type specifier' warnings for the rest of your life. Quite possibly beyond that. – Elise van Looij Jul 19 '20 at 19:58
9

You got this warning when somewhere in the file you used either _Nonnull, _Nullable, _Null_unspecified, __nullable, __nonnull, nullable or nonnull. Could also happen if the specifier was added through a macro.

To fix the warning:

  • you can remove all occurrences of nullability (not recommended)
  • you can assume all parameters and returned values are _Nonnull using NS_ASSUME_NONNULL_BEGIN and NS_ASSUME_NONNULL_END (recommended)
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • As far as I can tell this description is correct, but do you have a source with more detail on the intended semantics? The Clang documentation at https://clang.llvm.org/docs/DiagnosticsReference.html#wnullability-completeness describes the diagnostic text but not its actual meaning. – bcmills Dec 02 '21 at 15:58
  • The only definitive reference I have so far is the Clang implementation itself: https://github.com/llvm/llvm-project/blob/c9e46219f38da5c3fbfe41012173dc893516826e/clang/lib/Sema/SemaType.cpp#L4297-L4302 – bcmills Dec 02 '21 at 16:05