How To Fix Xcode's "used as the name of&

When you upgrade to Xcode 4.6, you may find that you are getting anumber of compiler warnings like this:

'xxxxx' used as the name of the previous parameter ratherthan as part of the selector

In this post I will show you why you're getting this warning, howto fix it, or how to ignore it.

Why You're Getting This Warning

This is typically caused by declaring a method without specifying adescription of the parameter. For example, check out the followingmethod declaration:

- (NSString *) getFormattedDateString:(NSDate *)date:(NSString*)formatString;

The intent of this code is to declare a methodnamed getFormattedDateString thataccepts two parametersnamed date and formatString.

Because there is no space between date and the colon (:), thecompiler gives the warning "'date' used as the name of the previousparameter rather than as part of the selector".

How to Fix The Problem

There are two ways to solve this warning:

1. Put a spacebetween date andthe colon:在date變量和冒號間加個空格

- (NSString *) getFormattedDateString:(NSDate *)date :(NSString*)formatString;

2. Add a description forthe formatString parameter:爲fotmatString參數加個描述

- (NSString *) getFormattedDateString:(NSDate *)datewithFormat:(NSString *)formatString;

The second option is preferable because it makes your method morereadable. That;s because parameter descriptions are part of themethod signature. In this example, the method signature is:

getFormattedDateString:withFormat:

If you leave out the parameter description, the method signature isthe less readable:

getFormattedDateString::

How to Ignore the Warning

Although it's preferable to fix these warnings by changing yourmethod declarations, if you want to ignore them, you can simply addthe following declaration to your project's PCH file (located inthe Support Files group):

#pragma clang diagnostic ignored "-Wmissing-selector-name"

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章