0

在实际写入 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 不友好,但我也读过它们被接受的案例。

4

1 回答 1

0

Why do you feel that swizzling is the best option here? Sounds to me like a subclass would be much better. There are some tricky parts about subclassing a stream but take a look here

If you are determined to make swizzling work, have you set breakpoints and made sure that your swizzling selectors are not nil? Might happen if you get the signatures a bit wrong

于 2014-11-29T08:03:42.400 回答