字符串不会显示在UITextView中

时间:2013-07-15 01:05:23

标签: ios objective-c cocoa-touch gcdasyncsocket

我似乎无法使UITextView显示字符串,在本例中为combined变量。

这是我的代码:

- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag {
    NSData *pongReply = [@"PONG :hades.arpa\r\n" dataUsingEncoding:NSASCIIStringEncoding];
    NSData *strData = [data subdataWithRange:NSMakeRange(0, [data length])];
    NSString *msg = [[NSString alloc] initWithData:strData encoding:NSASCIIStringEncoding];
    [asyncSocket readDataWithTimeout:-1 tag:0];
    NSLog(@"%@", msg);

    //ping reply
    NSRange pingReply = [msg rangeOfString:@"PING :hades.arpa"];
    NSRange chanMsgs = [msg rangeOfString:@"PRIVMSG #"];
    if (pingReply.location != NSNotFound) {
        NSLog(@"we got pinged");
        [asyncSocket writeData:pongReply withTimeout:-1 tag:1];
    }

    //Channel control / msgs
    if (chanMsgs.location != NSNotFound) {
        NSLog(@"CHANNEL MESSAGE!");
        //code for getting text after (space):
        NSString *str = msg;
        NSString *search = @" :";
        //code for before !~
        NSString *str2 = msg;
        NSString *search2 = @"!";
        NSString *sub = [str substringFromIndex:NSMaxRange([str rangeOfString:search])];
        NSString *sub2 = [str2 substringToIndex:NSMaxRange([str2 rangeOfString:search2])];

        NSString *sub2out = [sub2 stringByReplacingOccurrencesOfString:@":" withString:@""];
        NSString *sub2out2 = [sub2out stringByReplacingOccurrencesOfString:@"!" withString:@""];
        NSString *combined = [NSString stringWithFormat:@"%@: %@", sub2out2, sub];
        NSLog(@"%@", combined);
        chatWindow.text = [chatWindow.text stringByAppendingString:combined];
        chatWindow.text = [NSString stringWithFormat: @"%@", combined];
    }
}

任何可能出错的想法?​​

1 个答案:

答案 0 :(得分:0)

问题在于chatWindow - 或者:

  • nil
  • 它没有绑定到UITextView(因为它的'窗口'名称暗示)
  • 不可见(位置在窗口外或hidden为是)

检查那些。

一个简单的测试是替换你列出的整个计算:

chatWindow.text = @"Sample chat text";

如果失败,请详细查看chatWindow(在调试器中或NSLog (@"Window: %@", chatWindow)