【发布时间】: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,如果需要,可能有一个NSStringivar。 -
你是对的,应该在.m中导入它。结构差。重新扩展...好点,但没有任何区别...我调用的方法返回 NSString,这是我在寻找答案时所做的更改之一。原则上我会改回来的-谢谢你的戳。 :)
标签: ios objective-c ios7 methods parameters