ガイド「ソケットストリームの設定」に従い、クラスでそのコードを効果的に複製しました。デリゲートメソッドを何を試しても、呼び出されないようです。
私が持っているヘッダーファイルには(基本的に):
@interface myClass : NSObject <NSStreamDelegate> {
NSInputStream *inputStream;
NSOutputStream *outputStream;
}
- (void)connect;
@end;
接続方法:
- (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");
}
うまくいけば、私は本当に単純で明白な何かを見逃していて、2番目の目が私の間違いを見つけることができます。
忍耐強く、これまで読んでくれた人に感謝します!