How to remove this warning in templated code

17 views
Skip to first unread message

Eric Berman

unread,
May 14, 2014, 11:38:51 PM5/14/14
to wsdl2obj...@googlegroups.com
In ComplexType_M.template, there is this code:
- (id) initWithCoder:(NSCoder *)decoder {
    if ([super respondsToSelector:@selector(initWithCoder:)] && ![self isKindOfClass:[super class]]) {
        self = [super performSelector:@selector(initWithCoder:) withObject:decoder];
    } else {
        self = [super init];
    }
    if (self == nil) { return nil; }
...


XCode analyze returns a warning about this because of a potential code path here, where if the first condition is true, then self is assigned to something that is neither [super initWithCoder:decoder] or [super init].  This is actually not true, since either path in the if statement above obviously (to a human eye) hits one of these paths, but the static code analyzer does not recognize "[super performSelector@selector(initWithCoder) ...]" as being equivalent to "[super initWithCoder]"

In my private build, I did a simple hack: I inserted "self = [super init]" just before the "self = [super performSelector...]" line.  Works fine, suppresses the warning, but it has two potential flaws: (a) I'm calling init: then initWithCoder:, which is obviously a (small) performance hit doing unnecessary duplicate initialization, and (b) it's not guaranteed (AFAICT) that init followed by initWithCoder is the same as simply calling initWithCoder.

Sooo....thoughts about a better way to address (or suppress) the warning? 

Thanks

Henri Asseily

unread,
May 15, 2014, 12:51:10 AM5/15/14
to wsdl2obj...@googlegroups.com
I don’t understand why it was coded in that fashion. What about:

- (id) initWithCoder:(NSCoder *)decoder {
if ([super respondsToSelector:@selector(initWithCoder:)] && ![self isKindOfClass:[super class]]) {
self = [super initWithCoder:decoder];
} else {
self = [super init];
}
if (self == nil) { return nil; }
...

> --
> You received this message because you are subscribed to the Google Groups "WSDL2ObjC Discussion Group" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to wsdl2objc-disc...@googlegroups.com.
> To post to this group, send email to wsdl2obj...@googlegroups.com.
> Visit this group at http://groups.google.com/group/wsdl2objc-discuss.
> For more options, visit https://groups.google.com/d/optout.

Eric Berman

unread,
May 15, 2014, 10:08:25 AM5/15/14
to wsdl2obj...@googlegroups.com
I had tried that, but it simply replaces one warning with another: the template for the self object implements NSCoding, but derives from NSObject, which does not, so the compiler warns that "[super initWithCoder:decoder]" may not respond to the selector "initWithCoder" (despite the fact that we check at runtime in the prior line that in fact it does).

And the other thing I tried was casting super, but "super" is not a real object, so you can't cast it to anything.
Reply all
Reply to author
Forward
0 new messages