I am trying to subclass NSInputStream and NSOutputStream to keep track of the command I am passing to my server. This way, when I receive a response back from the server, I know which command it was a response to. When I try to set the command string in my subclass, I get an unrecognized selector error.
Subclasses:
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
The .m files just have the command property synthesized so I can call setCommand:
Here is the class I am using these in:
//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];
}
Here is the error I get when it runs the line
[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
Can someone suggest a simple solution for me? I tried wrapping a NSStreamInput instance and my NSString* command into another class, but that doesn’t work for my purposes.
Thanks!
You can’t just cast an
NSInputStreamto aPCFInputStream. The object has to be created as aPCFInputStream, andCFStreamCReatePairWithSocketToHostdoesn’t do that.You can just attach a
commandproperty toNSInputStreamin a category by using an associated object. Here’s the category interface:You implement it like this: