Re: Multiple methods named 'compare:' found
Re: Multiple methods named 'compare:' found
- Subject: Re: Multiple methods named 'compare:' found
- From: John McCall <email@hidden>
- Date: Thu, 27 Mar 2014 22:56:52 -0700
On Mar 27, 2014, at 10:40 PM, Gerriet M. Denkmann <email@hidden> wrote:
> I have:
>
> id a,b;
>
> if (something)
> {
> a = @(43);
> b = some other NSNumber;
> }
> else
> {
> a = [ NSDate date];
> b = some other NSDate;
> }
>
> if ( [ a compare: b] == NSOrderedDescending ) then ...
>
> Xcode Version 5.1 (5B130a) warns me that "Multiple methods named 'compare:' found".
>
> How do I switch off this warning (in this line only)?
> I do not want to switch off the warning for the whole file, just for this special line.
http://clang.llvm.org/docs/UsersManual.html#controlling-diagnostics-via-pragmas
But it’s almost certainly better practice to just restructure your code like so:
NSComparisonResult comparison;
if (something) {
comparison = [@(43) compare: some other NSNumber];
} else {
comparison = [[NSDate date] compare: some other NSDate];
}
if (comparison == NSOrderedDescending) then …
There are two reasons.
The first is that you aren’t subverting the type-checker and you get the full benefit of -Wstrict-selector-match (which is not something we enable in any of the standard groups, so you clearly do care about this on some level).
The second is that it gives you *much* more flexibility to change how you do the comparison, instead of being wedded to a single selector with no extra parameters.
If you can’t restructure the code in exactly that way because you’re just simplifying for the list, consider specifying a block or function pointer that does the comparison as part of your switch over the types.
John.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden