views:

35

answers:

2

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 *?

+2  A: 

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

<key>MessageDummy</key>
<string>Dummy\nMessage</string>

use:

<key>MessageDummy</key>
  <string>Dummy
          Message</string>

Hope this helps.

Madhup
+2  A: 

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.

Jeff