Xcode控制台打印格式

时间:2016-07-03 13:54:43

标签: ios objective-c xcode console

我有这种方法,当被叫:

- (void)showContent
{
NSLog(@" 0  1  2  3  4  5  6");
for (int j = 0; j < rows; j++) {
    for (int k = 0; k < columns; k++) {
        MDLog(@"[%c]", board[j][k]);
    }
    NSLog(@"");
}
NSLog(@"---------------------------------------------------");
}

这应该创建一个空的6 x 7网格(对于Connect Four游戏),如下所示:

0  1  2  3  4  5  6

[ ][ ][ ][ ][ ][ ][ ]

[ ][ ][ ][ ][ ][ ][ ]

[ ][ ][ ][ ][ ][ ][ ]

[ ][ ][ ][ ][ ][ ][ ]

[ ][ ][ ][ ][ ][ ][ ]

[ ][ ][ ][ ][ ][ ][ ]

---------------------------------------------------

然而,Xcode控制台根本不打印它。它将每列垂直显示在彼此之上。其他有用的信息是我

int rows; // initializes to be 6
int columns; // initializes to be 7
char board[6][7];

Xcode以这种方式打印:

 0  1  2  3  4  5  6

[ ]
[ ]
[ ]
[ ]
[ ]
[ ]
[ ]

[ ]
[ ]
[ ]
[ ]
[ ]
[ ]
[ ]

[ ]
[ ]
[ ]
[ ]
[ ]
[ ]
[ ]

[ ]
[ ]
[ ]
[ ]
[ ]
[ ]
[ ]

[ ]
[ ]
[ ]
[ ]
[ ]
[ ]
[ ]

[ ]
[ ]
[ ]
[ ]
[ ]
[ ]
[ ]

---------------------------------------------------

1 个答案:

答案 0 :(得分:0)

您可以连接每行的字符串并立即打印所有字符串。或者你可以使用printf()。 printf()不会添加换行符,因此会创建所需的结果。

- (void)showContent
{
    NSLog(@" 0  1  2  3  4  5  6");
    for (int j = 0; j < 6; j++) {
        for (int k = 0; k < 7; k++) {
            printf("[ ]");
        }
        printf("\n");
    }
    NSLog(@"---------------------------------------------------");

}