Brian F Love
Learn from a Google Developer Expert focused on Angular, Web Technologies, and Node.js from Portland, OR.
Learn Angular the right way with Ultimate Courses

Newlines stored in NSString attributes in Core Data

I recently ran into an interesting issue that I thought I would write about. I have an entity in Core Data that stores an explanation. The explanation attribute is a string type, and I am using this value to display the text within a UITextView. I have newline characters (n) in my data.

I was expecting the newlines to then be shown in the resulting UITextView when I set the text property to the value stored in my database. The problem is that the newlines were being displayed in the UITextView:

Newlines in UITextView

The simple solution is to replace instances of "n" with an actual newline character. It's a one-liner:

[explanation stringByReplacingOccurrencesOfString:@"\n" withString:@"n"];

Here we simply call the stringByReplacingOccurrencesOfString:withString: method on the string that we retrieved from core data.

Brian F Love

Hi, I'm Brian. I am interested in TypeScript, Angular and Node.js. I'm married to my best friend Bonnie, I live in Portland and I ski (a lot).