Q&A: HOW TO FIX XCODE’S "USED AS THE NAME OF THE PREVIOUS PARAMETER" WARNING

:问题的核心在于:没有按照Ob-C推荐的方法来声明多参数函数。省略了第二个或是后面参数的参数名,只用到一个冒号和行参名称,这样就造成编译器(Xcode 4.6及以上)警告。下文一共提供了三种方法来解决这一问题。

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

‘xxxxx’ used as the name of the previous parameter rather than as part of the selector

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

Why You’re Getting This Warning

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

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

The intent of this code is to declare a method named getFormattedDateString that accepts two parameters named date and formatString.

Because there is no space between date and the colon (:), the compiler gives the warning “‘date’ used as the name of the previous parameter rather than as part of the selector”.

How to Fix The Problem

There are two ways to solve this warning:

1. Put a space between date and the colon:

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

2. Add a description for the formatString parameter:

– (NSString *) getFormattedDateString:(NSDate *)date withFormat:(NSString *)formatString;

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

getFormattedDateString:withFormat:

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

getFormattedDateString::

How to Ignore the Warning

Although it’s preferable to fix these warnings by changing your method declarations, if you want to ignore them, you can simply add the following declaration to your project’s PCH file (located in the Support Files group):

#pragma clang diagnostic ignored “-Wmissing-selector-name”

这里还需要注意一点的是,Xcode 6.0及以上版本中创建工程时,为加快编译速度,已不默认添加.pch文件,这需要我们手动添加该文件。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值