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 ?
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]
.
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;
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
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With