在实际写入 NSOutputStream 之前,我需要编写自定义数据。
为了让 swizzling 代码执行,我创建了一个类别 NSOutputStream(SwizzleWrite),它包含以下内容:
SEL originalSelector = @selector(write:maxLength:);
SEL swizzledSelector = @selector(swizzledWrite:maxLength:);
Method originalMethod = class_getInstanceMethod([NSOutputStream class], originalSelector);
Method swizzledMethod = class_getInstanceMethod([self class], swizzledSelector);
method_exchangeImplementations(originalMethod, swizzledMethod);
然后我创建 Inout & 输出流: CFStreamCreatePairWithSocketToCFHost(kCFAllocatorDefault, hostRef, 80, &readStream, &writeStream);
inputStream = (__bridge_transfer NSInputStream *)readStream;
outputStream = (__bridge_transfer NSOutputStream *)writeStream;
[inputStream setDelegate:self];
[outputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
[outputStream open];
但是现在当控件到达handleEvent:delegate时,具体在:[outputStream write:rawstring maxLength:sizeof(rawstring)]; ,我在 swizzledWrite:maxLength 上没有得到它:
我在这里做错了什么?
PS:我已经读到 swizzling Apple 方法对 Appstore 不友好,但我也读过它们被接受的案例。