description方法中的换行不运行

我在重写description方法,在字符串中需要有换行,但是/n/r都不工作。

代码:

- (NSString *)description
{
    return [NSString stringWithFormat:@"some text: \r some more text - %@",@"more text"];
} 

然后我试了这样:

- (NSString *)description
{
    return [NSString stringWithFormat:@"some text: \n some more text - %@",@"more text"];
}

ChatGPT尝试为您解答,仅供参考
在字符串中使用 \n 或 \r 是有效的换行符。但是,当你使用 NSString 的 stringWithFormat: 方法来生成字符串时,这些转义序列不会起作用。


要在字符串中换行,你可以使用一个 Unicode 换行符,即 \u000A。你可以使用以下代码来实现换行:

- (NSString *)description
{
    return [NSString stringWithFormat:@"some text: \u000A some more text - %@", @"more text"];
}

这样,你就可以在返回的字符串中使用换行符了。

如果你想在字符串中使用多个换行符,你可以使用如下代码:

- (NSString *)description
{
    return [NSString stringWithFormat:@"some text: \u000A\u000A some more text - %@", @"more text"];
}

这将在字符串中插入两个换行符。