Xcode 4.6, used as the name of the previous parameter rather than as part of the selector [duplicate]
Asked Answered
H

4

32

I'm getting the following warning from Xcode 4.6.

.. used as the name of the previous parameter rather than as part of the selector

I know I can disable this warning, but I'd rather fix it.

I have 109 such warnings, so I'm obviously writing methods badly.

Here's a couple of my methods.

+(NSString*)addFormatPrice:(double)dblPrice:(BOOL)booRemoveCurSymbol;

-(void)showHelpChoices:(UIView *)vw:(id)dg;

So, whats the correct way to write these methods ?

Horn answered 2/2, 2013 at 8:29 Comment(1)
It is funny to see how an original question got only 12 upvotes whereas the duplicate got 29. Same case with answers too. Guess the more rep you have, more upvotes you get.Notorious
L
83

Your first method is declaring the selector +addFormatPrice::. With spaces, it looks like

+ (NSString *)addFormatPrice:(double)dblPrice :(BOOL)booRemoveCurSymbol;

This is invoked like [NSString addFormatPrice:0.3 :YES].

What you should do is actually give a name to the previous parameter, such as

+ (NSString *)addFormatPrice:(double)dblPrice removeCurSymbol:(BOOL)booRemoveCurSymbol;

Which would then be invoked like [NSString addFormatPrice:0.3 removeCurSymbol:YES].

Luminesce answered 2/2, 2013 at 8:37 Comment(3)
To fix the warning, it is sufficient to insert a space before the second parameter: -(void)showHelpChoices:(UIView *) vw:(id)dg;. Even if named parameters are the better choice, unnamed parameters are still legal Objective-C syntax.Gabrielgabriela
To fix the warning, it is sufficient to insert a space AFTER the second parameter and BEFORE the : symbol. -(void)showHelpChoices:(UIView *)vw :(id)dg;Between
Sure, if your goal is to just remove warnings. But the original code author wasn't intentionally trying to omit the parameter name, he just didn't know any better.Luminesce
D
14

Maybe you'll have an easier time understanding if you split these across several lines?

+(NSString*)addFormatPrice:(double)dblPrice
                          :(BOOL)booRemoveCurSymbol;

-(void)showHelpChoices:(UIView *)vw
                      :(id)dg;

An Objective-C method name's structure is like this:

- (returntype)firstPartOfMethodWithParameter:(type)nameOfFirstParameter secondPartOfNameWhichDescribesSecondParameter:(type)nameOfSecondParameter;

That is, the full method name is broken up, with the parameter names interspersed. The colons separate each "label" from its parameter; a space separates the parameter name from the next part of the method name.

Your methods are missing the second parts, the bits that describe the second parameters. Right now, the names of your methods are addFormatPrice:: and showHelpChoices::, both of which are legal but un-idiomatic. When you call them, it will look like this:

[Excelsior addFormatPrice:2.0 :YES];
[thumpy showHelpChoices:aView :obj];

which should make it clear that your names aren't quite right. You just need to add the labels for the second parameters:

+(NSString*)addFormatPrice:(double)dblPrice
    removingCurrencySymbol:(BOOL)booRemoveCurSymbol;

-(void)showHelpChoices:(UIView *)vw
             digeridoo:(id)dg;
Duramen answered 2/2, 2013 at 8:38 Comment(1)
"a space separates the parameter name from the next part of the method name": In fact inserting a space before the second parameter :(id)dg fixes the warning. (I agree that using names parameters is the better choice.)Gabrielgabriela
A
2

For advice on naming Objective-C methods, you should turn to an Objective-C style guide such as Apple's coding guidelines for Cocoa. Any style guide that follows the conventions of the community and Apple's frameworks will suggest that you name your method such that the purpose of each parameter is clearly described within the method name.

+(NSString *)priceStringWithPrice:(double)price removeCurrencySymbol:(BOOL)removeCurrencySymbol

-(void)showHelpChoicesInView:(UIView *)view withSomethingWithAnUndecipherableName:(id)mysteryParameter

Notice the significant change in name to indicate what (I assume) it does in your program and what each parameter does. Your class method doesn't add anything to anything - rather it returns a new string. This makes your code blend naturally with that of other developers, Apple's frameworks, other libraries you may use, and enhances the readability greatly. Not naming your parameters degrades readability and makes maintainability far more difficult.

On a related note, unnecessary abbreviations, including Hungarian notation, are jarring and don't fit the style, and if you follow good naming practices you don't need them and will produce code that is a pleasure to maintain. So don't call it vw, call it view or viewToShowIn. Don't call it strVal call it valueString or somethingSpecificallyDescribingTheNatureOfTheValueString.

Afghanistan answered 2/2, 2013 at 8:54 Comment(0)
C
1

This is how you are supposed to do this

+(NSString*)addFormatPrice:(double)dblPrice removeCurSymbol:(BOOL)booRemoveCurSymbol;

-(void)showHelpChoices:(UIView *)vw  whatEverThePurposeOf:(id)dg;

try to learn from Apple's example code.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

for start, you can try to write your method in a sentence.

like this

applicationdidFinishLaunchingWithOptions

then, add the noun description with parameters like (UIApplication *)application and (NSDictionary *)launchOptions

Critta answered 2/2, 2013 at 8:36 Comment(1)
I'd lowercase "RemoveCurSymbol" to "removeCurSymbol".Desorb

© 2022 - 2024 — McMap. All rights reserved.