NSSting * 和 .plist 中的 \n
代码:
NSString *strDirect = @"Dummy\nMessage";
NSString *strBundle = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"MessageDummy"];
NSLog(@"Direct: %@", strDirect);
NSLog(@"Bundle: %@", strBundle);
其中MessageDummy
是.plist文件中的字符串类型值Dummy\nMessage
。即:
<key>MessageDummy</key>
<string>Dummy\nMessage</string>
输出:
2010-08-09 11:49:18.641 Plan[15558:207] Direct: Dummy
Message
2010-08-09 11:49:18.642 Plan[15558:207] Bundle: Dummy\nMessage
\n
在 NSString *
中受到尊重,但在从 objectForKey
返回时则不然。如何使 objectForKey
的返回值的行为与 NSString *
相同?
Code:
NSString *strDirect = @"Dummy\nMessage";
NSString *strBundle = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"MessageDummy"];
NSLog(@"Direct: %@", strDirect);
NSLog(@"Bundle: %@", strBundle);
where MessageDummy
is a string type value Dummy\nMessage
in the .plist file. i.e.:
<key>MessageDummy</key>
<string>Dummy\nMessage</string>
Output:
2010-08-09 11:49:18.641 Plan[15558:207] Direct: Dummy
Message
2010-08-09 11:49:18.642 Plan[15558:207] Bundle: Dummy\nMessage
\n
is honored in NSString *
but not when returned from objectForKey
. How can I make the return value of objectForKey
behaves same as the NSString *
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是,当您尝试检索它时,“\n”被视为“\\n”(\ 用 \\ 转义)。解决这个问题的方法是,不要使用
use:
希望这会有所帮助。
The problem is that while "\n" is treated as "\\n" while you are trying to retrieve it (\ is escaped with \\). The work around for this is, instead of using
use:
Hope this helps.
除了 Madhup 的答案之外,可能值得指出的是“\n”是一个 C 编译器构造,一种用于传达在源代码中定义的文字字符串中包含换行符的愿望的机制。
Plist 以 XML 格式存储,它有自己的数据格式、转义规则等。
两者之间没有关系,您不应该期望两者之间有类似的行为。
In addition to Madhup's answer, its probably worth pointing out that "\n" is a C-compiler construct, a mechanism for communicating a desire to include a newline in a literal string defined in source code.
Plists are stored in XML format which has its own data format, escaping rules, etc.
There is no relationship between the two, and you should not expect similiar behaviour between the two.