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

时间:2013-10-26 23:35:36

标签: objective-c string nsstring nstextview nsmutablestring

#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

所以,我正在寻找一种改变[window setString:output]方法的雄辩方法。这是我试图编写的程序的.m文件,以便播放MUD(基于文本的游戏)。输入流表示与游戏服务器的连接,该游戏服务器发送我的客户信息(字节)。每次发生hasBytesAvailable事件时,字节都会转换为字符串。然后,这些字节显示在窗口的文本字段中。我遇到的问题是,每次事件发生时,它都会替换最初在屏幕上显示的内容。我试图创建一个占位符字符串,其中包含输出数据,然后附加到要显示的字符串上。它似乎不像我想要的那样工作。我希望能够不断地从流中获取数据,这些数据将在整个程序中逐行显示。我已经尝试过NSArray和NSMutableString ......我似乎无法弄清楚如何创建全局字符串变量,我可以在每次发生hasBytesAvailable事件时附加它。谢谢大家阅读,希望你能帮忙。

1 个答案:

答案 0 :(得分:0)

回答有关解析HTML标记的问题。查看 NSXMLParser NSXMLParserDelegate 以自行解析它们。另外,请看HTML parsing options