【问题标题】:iOS7 compiler won't recognize method call with messageiOS7 编译器无法识别带有消息的方法调用
【发布时间】:2014-09-22 21:48:08
【问题描述】:

所以这个让我难住了——可能很简单,但我一无所知。

我正在定义一个自定义类,其中包含一个接收一条消息(整数)的方法。调用该方法时,编译器拒绝识别我试图与调用一起发送的消息。 (“选择器'sendMessage:'没有已知的类方法。从调用和定义中删除消息 - 即从定义中删除:(int)mode,从调用中删除:1 - 允许它编译良好(但那么我当然会失去功能)。

我尝试将其定义为实例方法和类方法 - 两者都不起作用。

非常感谢您的集体智慧!

自定义类“Communications.h”:

@interface Communications : NSString

+(NSString*)sendMessage:(int)mode;

@end

Communications.m:

#import "Communications.h"

@interface Communications ()

@end

@implementation Communications

+(NSString*)sendMessage:(int)mode {

    // Do something important

}

ViewController.h:

#import "Communications.h"

- (void) tapPanic:(UITapGestureRecognizer*)sender;

ViewController.m:

- (void) tapPanic:(UITapGestureRecognizer *)sender {

    [Animations animatePanic:self.view type:0];
    panicactive = 1;
    NSString* tmpResponse = [Communications sendMessage:1];
    UILabel* tmpServerResponsePanic = [self.view viewWithTag:10002];
    tmpServerResponsePanic.text = tmpResponse;
    [[self serverResponsePanic] setNeedsDisplay];

}

【问题讨论】:

  • 这可能无法解决问题,但为什么要在“ViewController.h”中导入“Communications.h”?只需将其导入“ViewController.m”即可。
  • 顺便说一句 - 为什么 Communications 扩展 NSString?这不是一个好的设计,因为Communications 类不是专门的NSString。它应该扩展NSObject,如果需要,可能有一个NSString ivar。
  • 你是对的,应该在.m中导入它。结构差。重新扩展...好点,但没有任何区别...我调用的方法返回 NSString,这是我在寻找答案时所做的更改之一。原则上我会改回来的-谢谢你的戳。 :)

标签: ios objective-c ios7 methods parameters


【解决方案1】:

因此,使用 Xcode 将其归结为怪异...将 Communications .h 和 .m 的内容复制/粘贴到新文件中,并使用新的类定义 (Comms) 就可以了。我认为编译器很困惑,并且记住了该方法的旧定义。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 2017-10-11
    相关资源
    最近更新 更多