I have activated the new compiler warning CLANG_WARN_NULLABLE_TO_NONNULL_CONVERSION
. I could resolve most of the warnings, but one stems from a macro, and I'm not sure how to resolve the issue.
The macro looks like this:
#define MYAssertionFail(...) [[NSAssertionHandler currentHandler] handleFailureInFunction:[NSString stringWithCString:__PRETTY_FUNCTION__ encoding:NSUTF8StringEncoding] file:[NSString stringWithCString:__FILE__ encoding:NSUTF8StringEncoding] lineNumber:__LINE__ description:__VA_ARGS__]
I get the following error:
Implicit conversion from nullable pointer 'NSString * _Nullable' to non-nullable pointer type 'NSString * _Nonnull'
Any idea how to rewrite the macro?
I already tried __PRAGMA_PUSH_NO_EXTRA_ARG_WARNINGS/__PRAGMA_POP_NO_EXTRA_ARG_WARNINGS
but it did not help.
First:
- (void)handleFailureInFunction:(NSString *)functionName file:(NSString *)fileName lineNumber:(NSInteger)line description:(nullable NSString *)format,... NS_FORMAT_FUNCTION(4,5);
is wrapped in NS_ASSUME_NONNULL_BEGIN
and NS_ASSUME_NONNULL_END
This means the compiler resolves this function declaration as:
- (void)handleFailureInFunction:(NSString * _Nonnull)functionName file:(NSString * _Nonnull)fileName lineNumber:(NSInteger)line description:(nullable NSString *)format,... NS_FORMAT_FUNCTION(4,5);
Second
+ (nullable id)stringWithCString:(const char *)bytes NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);
This function returns a nullable id, translated this is a NSString * _Nullable
To conclude
It should be enough to add a cast in your function call like so:
#define MYAssertionFail(...) [[NSAssertionHandler currentHandler] handleFailureInFunction:(NSString * _Nonnull)[NSString stringWithCString:__PRETTY_FUNCTION__ encoding:NSUTF8StringEncoding] file:(NSString * _Nonnull)[NSString stringWithCString:__FILE__ encoding:NSUTF8StringEncoding] lineNumber:__LINE__ description:__VA_ARGS__]
If you want to read up on nullability in Obj-C and Swift, please read this Apple blog post about this matter: https://developer.apple.com/swift/blog/?id=25