我已经按照设置套接字流指南,在我的类中有效地复制了该代码。但无论我尝试什么,委托方法似乎都没有被调用。
在头文件中,我有(基本上):
@interface myClass : NSObject <NSStreamDelegate> {
NSInputStream *inputStream;
NSOutputStream *outputStream;
}
- (void)connect;
@end;
connect方法:
- (void)connect {
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (CFStringRef)@"host.example.com", 1234, &readStream, &writeStream);
inputStream = (NSInputStream *)readStream;
outputStream = (NSOutputStream *)writeStream;
[inputStream setDelegate:self];
[outputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
[outputStream open];
}
尝试过使用CFStreamCreatePairWithSocketToCFHost()
和[NSStream getStreamsToHost:port:inputStream:outputStream:
,结果完全相同。
我在connect
方法的开头设置了断点,逐行执行过程并检查了每个指针,都是有效的且似乎指向了正确的对象。
在GDB中,在调用setDelegate
之后,打印po [inputStream delegate]
可以按预期显示为<myClass: 0x136380>
,因此已正确设置代理。
我真的想不出为什么它拒绝在我的类上调用stream:handleEvent:
方法:
- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode {
NSLog(@"got an event");
}