I\'m writing a property list to be in the resources bundle of my application. An NSString object in the plist needs to have line-breaks in it. I tried \\n
a little late, but i discovered the same issue and i also discovered a fix or workaround. so for anyone who stumbles on this will get an answer :)
so the problem is when you read a string from a file, \n will be 2 characters unlike in xcode the compiler will recognize \n as one.
so i extended the NSString class like this:
"NSString+newLineToString.h":
@interface NSString(newLineToString)
-(NSString*)newLineToString;
@end
"NSString+newLineToString.m":
#import "NSString+newLineToString.h"
@implementation NSString(newLineToString)
-(NSString*)newLineToString
{
NSString *string = @"";
NSArray *chunks = [self componentsSeparatedByString: @"\\n"];
for(id str in chunks){
if([string isEqualToString:@""]){
string = [NSString stringWithFormat:@"%@",str];
}else{
string = [NSString stringWithFormat:@"%@\n%@",string,str];
}
}
return string;
}
@end
How to use it:
rootDict = [[NSDictionary alloc]initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"yourFile" ofType:@"plist"]];
NSString *string = [[rootDict objectForKey:@"myString"] newLineToString];
its quick and dirty, be aware that \\n in your file will not be recognize as \n so if you need to write \n on text you have to modify the method :)