【问题标题】:"warning: '<CLASS>'may not respond to '<[-|+]FUNCTION>'" Objective C/Xcode Compiler Warnings“警告:'<CLASS>' 可能不响应 '<[-|+]FUNCTION>'” 目标 C/Xcode 编译器警告
【发布时间】:2010-03-09 18:58:33
【问题描述】:

我想出了这个,但认为它值得拥有自己的问答对。

我是 Xcode 和 Objective C 的新手,并且开始了解它的各种怪癖。例如,当我尝试编译以下代码时,会出现编译器警告“警告:''可能无法响应''”,这些代码都出现在我的实现文件中,因为我希望创建一个私有的这个类的静态效用函数:

// Here's the function declaration in the implementation file (I don't want it in the header)
+(void)authenticationRedirectTo:(NSURL *)url WithRelayState:(NSString *)relayState AndSAMLResponse:(NSString *)samlResponse {
...
}

...

// Later on, here's a call to that same function:
[CnaCalendarController authenticationredirectTo:formActionURL WithRelayState:relayState AndSAMLResponse:SAMLResponse];
...

编译时,会产生上述警告。接下来,我将发布我的解决方案。也欢迎提出您的想法!

【问题讨论】:

    标签: objective-c xcode compiler-warnings


    【解决方案1】:

    如果你真正想要的是一个私有方法,即你不希望该方法在头文件中,那么我喜欢使用一个Category来完成这个。我只是在我的实现之上定义了类别。

    // Enforce private methods by putting them in a category.
    @interface YourClass (PrivateMethods)
    
    +(void)authenticationRedirectTo:(NSURL *)url WithRelayState:(NSString *)relayState AndSAMLResponse:(NSString *)samlResponse;
    
    @end
    
    @implementation YourClass
    
    +(void)authenticationRedirectTo:(NSURL *)url WithRelayState:(NSString *)relayState AndSAMLResponse:(NSString *)samlResponse {
    ...
    }
    @end
    

    现在,你的方法在你的实现中的顺序是什么并不重要,这样你就可以正确地“#pragma mark”

    【讨论】:

    • 您甚至不必命名您的类别。 @interface YourClass () 应该可以工作。
    【解决方案2】:

    问题在于您的方法调用:

    [CnaCalendarController authenticationredirectTo:formActionURL WithRelayState:relayState AndSAMLResponse:SAMLResponse];
    

    请注意,“authenticationredirectTo:...”中的第一个“r”是小写字母,但您已将其声明为“authenticationRedirectTo:...”,并带有大写“R”。鉴于此,编译器抱怨找不到您正在调用的方法的声明也就不足为奇了。代码也可能会在该行崩溃,因为未定义带有小写 'r' 的方法。

    【讨论】:

      【解决方案3】:

      我认为问题在于,您在调用此函数后声明了该函数。编译时找不到它。

      【讨论】:

        【解决方案4】:

        好的,这是我承诺的解决方案:

        问题 1:当函数声明或实现出现在已处理源代码中的调用之后时,Xcode 将生成错误警告。就我而言,它们位于同一个文件中,因此我能够转到调用上方的函数实现。

        还要检查导入的顺序,以确保在调用它的导入之前导入此类函数。我没有看到这个,但看到其他帖子是这种情况。

        问题 2:Xcode 似乎对函数名称的 LENGTH 有一些限制。如下图所示,缩短我的函数名称解决了这个问题。我显然会选择更有意义的东西。

        // Here is the warning function commented out and a *shorter* name in place.
        //+(void)authenticationRedirectTo:(NSURL *)url WithRelayState:(NSString *)relayState AndSAMLResponse:(NSString *)samlResponse {
        +(void)X:(NSURL *)url Y:(NSString *)relayState Z:(NSString *)samlResponse {
        

        希望这可以帮助您解决问题。如果有用,别忘了给这个答案投票。

        【讨论】:

        • 我同意你对你的问题的解决方案1:C语言关心顺序。这不是错误的,它只是 C 规范的一部分!我认为您对问题 2 的解决方案有问题。名称 authenticationRedirectTo:WithRelayState:AndSAMLResponse: 在标准 Cocoa 中的长度并不长。你真的输入正确吗,保持大写/小写的区别?源文件和警告是什么?
        • Xcode 绝对没有对函数名长度的限制。你犯了一个错误。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-01
        • 2011-01-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多