[Xcode] lots of analyzer issues with Xcode 7 beta
With Xcode 7 beta (7A120f), the analyzer has a lot of warnings. These are mostly related to designated initializer overrides or incorrectly calling (or not) the inherited designated initializer.
cc @nicklockwood @tadeuzagallo @a2
Thanks, I'll take a look.
I solved this in my own components with a couple extra method declarations. In the header file I have:
NS_ASSUME_NONNULL_BEGIN
...
- (instancetype)initWithFrame:(CGRect)frame NS_UNAVAILABLE;
- (instancetype)initWithCoder:(NSCoder *)coder NS_UNAVAILABLE;
- (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher NS_DESIGNATED_INITIALIZER;
...
NS_ASSUME_NONNULL_END
The implementation file has:
- (instancetype)initWithFrame:(CGRect)frame
{
@throw EXNotImplementedException();
}
- (instancetype)initWithCoder:(NSCoder *)coder
{
@throw EXNotImplementedException();
}
That macro is defined as:
#define EXNotImplementedException() \
[NSException exceptionWithName:EXNotImplementedException \
reason:[NSString stringWithFormat:@"-[%s %s] is not implemented", \
class_getName([self class]), \
sel_getName(_cmd)] \
userInfo:@{@"file": @(__FILE__), @"line": @(__LINE__)}];
I'd be interested to learn of a leaner solution. It's kind of disappointing that NS_UNAVAILABLE alone doesn't suffice.
(Aside: with the nonnull keyword from Xcode 6.3, init methods no longer need the if (self = [super init]) dance.)
Hmm, well this is better than my solution, which was to have every unsupported initializer call the designated init with nil params, and then param assert that they mustn't be nil.
I'm surprised that it's no longer necessary to return self from init though - that's awesome! Is there a document about this change somewhere?
Btw, you can print [self class] directly using %@ - no need to use class_getName.
Did you try using [self doesNotRecognizeSelector:_cmd]? - I believe that's a common pattern for unsupported method implementations.
Oh I see, you don't need to return because of the @throw, and you don't need the if self because of the ASSUME_NON_NULL. Makes sense.
have every unsupported initializer call the designated init with nil params, and then param assert that they mustn't be nil.
This wouldn't work for init methods that have nonnullable parameters. In my example, - (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher actually desugars to (nonnull RCTEventDispatcher *) so it's not clear how to call it from initWithCoder/Frame.
I'm surprised that it's no longer necessary to return self from init though
Yeah I was referring to the 'if' check. Still nice to remove the extra level of indentation.
Did you try using [self doesNotRecognizeSelector:_cmd]? - I believe that's a common pattern for unsupported method implementations.
Just tried it now but the compiler doesn't recognize that [self doesNotRecognizeSelector:_cmd] affects the program flow. I had the same problem with [NSException raise:] earlier so I had to use @throw to tell the compiler that the init method doesn't return (I kept it outside of the macro to communicate this to the reader as well).
One thing we could do to ease the burden is to have RCTView revoke the "designated initializer" status from initWithCoder so view subclasses only need to deal with initWithFrame if they introduce a new designated initializer.
Hi there! This issue is being closed because it has been inactive for a while.
But don't worry, it will live on with ProductPains! Check out it's new home: https://productpains.com/post/react-native/xcode-lots-of-analyzer-issues-with-xcode-7-beta