我正在尝试继承 NSInputStream 和 NSOutputStream 以跟踪我传递给服务器的命令。这样,当我收到来自服务器的响应时,我知道它是对哪个命令的响应。当我尝试在子类中设置命令字符串时,出现无法识别的选择器错误。
子类:
PCFInputStream.h
#import <Foundation/Foundation.h>
@interface PCFInputStream : NSInputStream
@property (nonatomic, strong) NSString *command;
@end
PCFOutputStream.h
#import <Foundation/Foundation.h>
@interface PCFOutputStream : NSOutputStream
@property (nonatomic, strong) NSString *command;
@end
.m 文件只是合成了命令属性,因此我可以调用 setCommand:
这是我在其中使用这些的课程:
//instance vars in my class
PCFInputStream *inputStream;
PCFOutputStream *outputStream;
-(void)followClass:(id)sender
{
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef) SERVER_ADDRESS, PORT, &readStream, &writeStream);
inputStream = (__bridge_transfer PCFInputStream *)readStream;
outputStream = (__bridge_transfer PCFOutputStream *)writeStream;
[inputStream setDelegate:self];
[outputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream setCommand:[NSString stringWithFormat:@"%d,%@",[sender tag], [class classTitle]]];
[inputStream open];
NSString *str = [NSString stringWithFormat:@"_ADD_CLASS*%@*%@*%@*%@;", [class CRN], deviceToken, [class classLink],[class courseNumber]];
[outputStream setCommand:str];
[outputStream open];
}
这是我在运行该行时遇到的错误
[inputStream setCommand:[NSString stringWithFormat:@"%d,%@",[sender tag], [class classTitle]]];
-[__NSCFInputStream setCommand:]: unrecognized selector sent to instance 0x1e691170
2012-12-01 21:56:20.777 PCF[15610:907] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFInputStream setCommand:]: unrecognized selector sent to instance 0x1e691170'
*** First throw call stack:
(0x3a1b73e7 0x39043963 0x3a1baf31 0x3a1b964d 0x3a111208 0xfa267 0x39585047 0x39584ffb 0x39584fd5 0x3958488b 0x39584d79 0x394a3441 0x3a18c941 0x3a18ac39 0x3a18af93 0x3a0fe23d 0x3a0fe0c9 0x3743733b 0x394ee291 0x100005 0xe7d48)
libc++abi.dylib: terminate called throwing an exception
有人可以为我建议一个简单的解决方案吗?我尝试将 NSStreamInput 实例和我的 NSString* 命令包装到另一个类中,但这对我的目的不起作用。
谢谢!