简体   繁体   English

将表示我的格式说明符的字符串传递给stringWithFormat并查看问题

[英]Passing a string representing my format specifier into stringWithFormat and seeing issues

If I call "[NSString stringWithFormat:@"Testing \\n %@",variableString]"; 如果我调用“ [NSString stringWithFormat:@“正在测试\\ n%@”,variableString]“; I get what I would expect, which is Testing, followed by a new line, then the contents of variableString. 我得到了我期望的结果,它是Testing,然后是新行,然后是variableString的内容。

However, if i try NSString *testString = @"Testing \\n %@"; 但是,如果我尝试使用NSString * testString = @“ Testing \\ n%@”; //forgive shorthand here [NSString stringWithFormat,testString,variableString] //在这里原谅速记[NSString stringWithFormat,testString,variableString]

the output actually literally writes \\n to the screen instead of a newline. 输出实际上将\\ n而不是换行符写入屏幕。 any workaround to this? 任何解决方法? seems odd to me 对我来说似乎很奇怪

What you describe is not normally the case. 您所描述的通常不是这种情况。 The \\n is translated into a newline by the compiler — the program will never see that you wrote it as \\n . 编译器将\\n转换为换行符-程序将永远不会看到您将其编写为\\n

However, if you're actually trying to use a string you've obtained from somewhere else (that is, you didn't really write it in your source file) that literally contains the character sequence "\\n", yes, it will be printed literally. 但是,如果您实际上是在尝试使用从其他位置获得的字符串(也就是说,您实际上并没有在源文件中写入该字符串),而该字符串确实包含字符序列“ \\ n”,是的,它将从字面上打印。 In that case you will need to replace the "\\n" with a newline character yourself. 在这种情况下,您需要自己用换行符替换“ \\ n”。

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

相关问题 如何使用stringWithFormat将连字符动态添加到我的字符串中 - how to use stringWithFormat to dynamically add hyphens into my string 为什么会看到错误“格式不是字符串文字且没有格式参数”? - Why am I seeing the error “Format is not a string literal and no format arguments”? stringWithFormat生成带有乱码字符的字符串 - stringWithFormat produces string with gibberish characters NS String比较因stringWithFormat而失败 - NS String comparison fails with stringWithFormat 带有[NSString stringWithFormat:]的Nil字符串显示为“(null)” - Nil string with [NSString stringWithFormat:] appears as “(null)” 使用NSString stringWithFormat:使用来自NSDictionary的字符串 - Using NSString stringWithFormat: with a string from NSDictionary NSString stringWithFormat swizzled允许缺少格式编号的args - NSString stringWithFormat swizzled to allow missing format numbered args 是否有适用于 Boolean 值的格式说明符? - Is there a format specifier that works with Boolean values? 从CLLocationDegrees生成字符串,例如在NSLog或StringWithFormat中 - Generating a String from CLLocationDegrees, e.g. in NSLog or StringWithFormat Objective-C使用stringWithFormat和%d“错误广播”字符串/整数 - Objective-C “miscasting” a string/int using stringWithFormat and %d
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM