子类化NSStream

时间:2012-12-02 02:58:20

标签: objective-c ios cocoa-touch

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

谢谢!

2 个答案:

答案 0 :(得分:3)

您无法将NSInputStream投射到PCFInputStream。该对象必须创建为PCFInputStream,而CFStreamCReatePairWithSocketToHost不会这样做。

您只需使用associated objectcommand属性附加到某个类别的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

答案 1 :(得分:1)

CFStreamCreatePairWithSocketToHost创建CFWriteStreamCFReadStream。这些是免费的,分别与NSOutputStreamNSInputStream联系在一起。所以你得到的是NSOutputStreamNSInputStream。你是否宣布了这些子类别没有区别;这些是CFStreamCreatePairWithSocketToHost创建的类。

如果您因任何原因无法使用包装,则需要重新实现Core Foundation功能以创建自定义子类。在实践中,我建议你只需要包裹NS[Output/Input]Stream并使用forwardingTargetForSelector:,这样你就可以在任何原始对象可以接受的地方提供你的包装。

相关问题