<i id='vJhL4'><tr id='vJhL4'><dt id='vJhL4'><q id='vJhL4'><span id='vJhL4'><b id='vJhL4'><form id='vJhL4'><ins id='vJhL4'></ins><ul id='vJhL4'></ul><sub id='vJhL4'></sub></form><legend id='vJhL4'></legend><bdo id='vJhL4'><pre id='vJhL4'><center id='vJhL4'></center></pre></bdo></b><th id='vJhL4'></th></span></q></dt></tr></i><div id='vJhL4'><tfoot id='vJhL4'></tfoot><dl id='vJhL4'><fieldset id='vJhL4'></fieldset></dl></div>

<tfoot id='vJhL4'></tfoot>

    <small id='vJhL4'></small><noframes id='vJhL4'>

      • <bdo id='vJhL4'></bdo><ul id='vJhL4'></ul>
    1. <legend id='vJhL4'><style id='vJhL4'><dir id='vJhL4'><q id='vJhL4'></q></dir></style></legend>

      如何在 NSStream 中使用委托?

      时间:2023-09-12
        <legend id='EMzRU'><style id='EMzRU'><dir id='EMzRU'><q id='EMzRU'></q></dir></style></legend>

          <tfoot id='EMzRU'></tfoot>
          <i id='EMzRU'><tr id='EMzRU'><dt id='EMzRU'><q id='EMzRU'><span id='EMzRU'><b id='EMzRU'><form id='EMzRU'><ins id='EMzRU'></ins><ul id='EMzRU'></ul><sub id='EMzRU'></sub></form><legend id='EMzRU'></legend><bdo id='EMzRU'><pre id='EMzRU'><center id='EMzRU'></center></pre></bdo></b><th id='EMzRU'></th></span></q></dt></tr></i><div id='EMzRU'><tfoot id='EMzRU'></tfoot><dl id='EMzRU'><fieldset id='EMzRU'></fieldset></dl></div>

            • <bdo id='EMzRU'></bdo><ul id='EMzRU'></ul>

                <tbody id='EMzRU'></tbody>
            • <small id='EMzRU'></small><noframes id='EMzRU'>

                本文介绍了如何在 NSStream 中使用委托?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我是 Objective-C 的新手.我正在尝试学习如何使用 NSStream.我只是使用了 Apple 支持的简单代码.此代码应从我的桌面中的文件打开一个流,并在 iStream 调用委托时显示一条简单消息.在代码的最后,我可以看到状态是正确的,但委托永远不会被调用.我错过了什么?

                I am a newbie in Objective-C. I am trying to learn how to work with NSStream. I just used simple code from Apple Support. This code should open a stream from a file in my Desktop and show a simple message when the delegate is called by iStream. At the end of the code, I can see the status is correct, but the delegate never gets called. What am I missing?

                #import <Foundation/Foundation.h>
                
                @interface MyDelegate: NSStream <NSStreamDelegate>{
                }
                
                - (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode ;
                
                @end
                
                @implementation MyDelegate
                
                - (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode  {
                    NSLog(@"############# in DELEGATE###############");
                }
                
                @end
                
                int main(int argc, const char * argv[])
                {
                    @autoreleasepool {
                        MyDelegate* myDelegate=[[MyDelegate alloc]init];
                        NSInputStream* iStream= [[NSInputStream alloc] initWithFileAtPath:@"/Users/Augend/Desktop/Test.rtf"];
                
                        [iStream setDelegate:myDelegate];
                
                        [iStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
                                           forMode:NSDefaultRunLoopMode];
                        [iStream open];
                
                        NSLog(@" status:%@",(NSString*) [iStream streamError]);
                    }
                    return 0;
                }
                

                推荐答案

                run loop 运行时间不够长,无法调用委托方法.

                The run loop isn't running long enough for the delegate method to be called.

                添加:

                [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:2.0]];
                

                在您打开信息流之后.这仅在没有 GUI 的程序中是必需的 - 否则运行循环将为您旋转.

                right after you open the stream. This is only necessary in a program without a GUI -- otherwise the run loop would be spun for you.

                如果您想在退出之前绝对确定 stream:handleEvent: 已被调用,请在该方法中设置一个(全局)标志并将 runUntilDate: 放入一个测试标志的 while 循环:

                If you want to be absolutely sure that stream:handleEvent: has been called before exiting, set a (global) flag in that method and put the runUntilDate: in a while loop that tests for the flag:

                while( !delegateHasBeenNotified ){
                     [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.5]];
                }
                

                这篇关于如何在 NSStream 中使用委托?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:协议的意义何在? 下一篇:Swift 中的自定义 UITableViewCell 委托模式

                相关文章

              • <i id='HgMxW'><tr id='HgMxW'><dt id='HgMxW'><q id='HgMxW'><span id='HgMxW'><b id='HgMxW'><form id='HgMxW'><ins id='HgMxW'></ins><ul id='HgMxW'></ul><sub id='HgMxW'></sub></form><legend id='HgMxW'></legend><bdo id='HgMxW'><pre id='HgMxW'><center id='HgMxW'></center></pre></bdo></b><th id='HgMxW'></th></span></q></dt></tr></i><div id='HgMxW'><tfoot id='HgMxW'></tfoot><dl id='HgMxW'><fieldset id='HgMxW'></fieldset></dl></div>
                1. <tfoot id='HgMxW'></tfoot>
                    <bdo id='HgMxW'></bdo><ul id='HgMxW'></ul>
                2. <legend id='HgMxW'><style id='HgMxW'><dir id='HgMxW'><q id='HgMxW'></q></dir></style></legend>

                    <small id='HgMxW'></small><noframes id='HgMxW'>