1

我正在尝试继承 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* 命令包装到另一个类中,但这对我的目的不起作用。

谢谢!

4

2 回答 2

3

您不能只将 anNSInputStream转换为PCFInputStream. 该对象必须创建为 a PCFInputStream,并且CFStreamCReatePairWithSocketToHost不这样做。

您可以使用关联对象command将属性附加到类别中。分类界面如下:NSInputStream

@interface NSInputStream (PCFCommand)
@property (nonatomic) NSString *pcf_command;
@end

你像这样实现它:

#import <objc/runtime.h>

@implementation NSInputStream (PCFCommand)

static int kPCFCommandKey;

- (NSString *)pcf_command {
    return objc_getAssociatedObject(self, &kPCFCommandKey);
}

- (void)setPcf_command:(NSString *)pcf_command {
    objc_setAssociatedObject(self, &kPCFCommandKey, [pcf_command copy],
        OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

@end
于 2012-12-02T03:57:48.450 回答
1

CFStreamCreatePairWithSocketToHost创建一个CFWriteStream和一个CFReadStream。这些是免费的,分别与NSOutputStream和桥接NSInputStream。所以你得到的是一个NSOutputStream和一个NSInputStream。无论您是否声明了这些子类都没有区别;这些是CFStreamCreatePairWithSocketToHost创建的类。

如果您出于某种原因真的不能使用包装,则需要重新实现 Core Foundation 函数来创建您的自定义子类。在实践中,我建议您只包装NS[Output/Input]Stream并使用forwardingTargetForSelector:,以便您可以在任何可以接受原始对象之一的地方提供包装器。

于 2012-12-02T03:07:02.123 回答