简体   繁体   English

如何正确显示到NSTextView中? NSString / NSInputStream / NSTextView

[英]How do I display into an NSTextView properly? NSString / NSInputStream / NSTextView

#import "AppDelegate.h"





@implementation AppDelegate
@synthesize inputStream;
@synthesize outputStream;
@synthesize textField;
@synthesize window;
@synthesize aText;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [self initNetworkCommunication];

}

- (void)initNetworkCommunication {
    CFReadStreamRef readStream;
    CFWriteStreamRef writeStream;
    CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"tec.skotos.net", 6730, &readStream, &writeStream);
    inputStream = (__bridge NSInputStream *)readStream;
    outputStream = (__bridge NSOutputStream *)writeStream;
    [inputStream setDelegate:self];
    [outputStream setDelegate:self];
    [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [inputStream open];
    [outputStream open];
    NSString *response = [NSString stringWithFormat:@"/\\/connect: n/a!!n/a"];
    NSData *data = [[NSData alloc] initWithData:[response dataUsingEncoding:NSASCIIStringEncoding]];
    NSString *end = @"\n";
    NSData *endData = [[NSData alloc] initWithData:[end dataUsingEncoding:NSASCIIStringEncoding]];
    [outputStream write:[data bytes] maxLength:[data length]];
    [outputStream write:[endData bytes] maxLength:[endData length]];



}



- (IBAction)sendMessage:(id)sender {
    NSString *response = [textField stringValue ];
    NSData *data = [[NSData alloc] initWithData:[response dataUsingEncoding:NSASCIIStringEncoding]];
    NSString *end = @"\n";
    NSData *endData = [[NSData alloc] initWithData:[end dataUsingEncoding:NSASCIIStringEncoding]];
    [outputStream write:[data bytes] maxLength:[data length]];
    [outputStream write:[endData bytes] maxLength:[endData length]];
}


- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {

    switch (streamEvent) {

        case NSStreamEventOpenCompleted:
            NSLog(@"Stream opened");
            break;
        case NSStreamEventHasSpaceAvailable:
            NSLog(@"Has space");
            break;

        case NSStreamEventHasBytesAvailable:

            if (theStream == inputStream) {
                uint8_t buffer[900000];
                long len;

                while ([inputStream hasBytesAvailable]) {
                    len = [inputStream read:buffer maxLength:sizeof(buffer)];
                    if (len > 0) {

                        NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];

                        NSLog(@"Has Bytes");
                        if (nil != output) {
                            NSLog(@"%@", output);
                            [window setString:output];



                        }
                    }
                }
            }
            break;

        case NSStreamEventEndEncountered:
            NSLog(@"Error.");
            break;

    }

}

@end

So, I'm looking for an eloquent way to change the [window setString:output] method. 因此,我正在寻找一种有效的方法来更改[window setString:output]方法。 This is the .m file for a program that I am trying to write in order to play a MUD (a text based game). 这是我尝试编写的程序的.m文件,以便播放MUD(基于文本的游戏)。 The inputstream represents a connection to the game server which sends my client information (bytes). 输入流表示与游戏服务器的连接,该服务器发送我的客户信息(字节)。 The bytes are converted to strings everytime the hasBytesAvailable event occurs. 每当hasBytesAvailable事件发生时,字节就会转换为字符串。 These bytes are then displayed on the window's text field. 这些字节然后显示在窗口的文本字段中。 The problem I am having is that each time the event occurs it replaces what was initially on the screen. 我遇到的问题是,每次事件发生时,它都会替换屏幕上最初显示的内容。 I tried to create a placeholder string that held the output data then append onto that string to display. 我试图创建一个保留输出数据的占位符字符串,然后追加到该字符串以显示。 It does not seem to work the way I want it. 它似乎没有按照我想要的方式工作。 I would like to be able to constantly get data from the stream which will display line by line continuously throughout the program. 我希望能够不断从流中获取数据,该流将在整个程序中连续不断地显示。 I've tried NSArray and NSMutableString... I can't seem to figure out how to create the global string variable that I can just append every time the hasBytesAvailable event occurs. 我已经尝试过NSArray和NSMutableString ...我似乎无法弄清楚如何创建全局字符串变量,每次hasBytesAvailable事件发生时我都可以附加该变量。 Thank you all for reading and I hope you can help. 谢谢大家的阅读,希望能对您有所帮助。

To answer your question on parsing HTML tags. 回答有关解析HTML标签的问题。 Look into NSXMLParser and NSXMLParserDelegate to parse them yourself. 查看NSXMLParserNSXMLParserDelegate自己解析它们。 Else take a look at HTML parsing options 其他看看HTML解析选项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM