react-native icon indicating copy to clipboard operation
react-native copied to clipboard

[Xcode] lots of analyzer issues with Xcode 7 beta

Open jwyld opened this issue 11 years ago • 8 comments

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.

jwyld avatar Jun 10 '15 14:06 jwyld

cc @nicklockwood @tadeuzagallo @a2

ide avatar Jun 11 '15 04:06 ide

Thanks, I'll take a look.

nicklockwood avatar Jun 11 '15 07:06 nicklockwood

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.)

ide avatar Jun 12 '15 02:06 ide

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?

nicklockwood avatar Jun 12 '15 07:06 nicklockwood

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.

nicklockwood avatar Jun 12 '15 08:06 nicklockwood

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.

nicklockwood avatar Jun 12 '15 08:06 nicklockwood

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.

ide avatar Jun 12 '15 08:06 ide

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

brentvatne avatar Jan 02 '16 02:01 brentvatne