How to read the contents of a text file into an NSString

Imagine we had a file called myfile.txt which is a standard text file. On each line we have a new item we’d like to read so that our app can do something with it. Here’s how we do that:

It is likely that instead of a long list of text we’d rather have each line in a separate string.
Thankfully there’s a method for this:

About Jay Versluis

Jay is a medical miracle known as Super Survivor. He runs two YouTube channels, five websites and several podcast feeds. To see what else he's up to, and to support him on his mission to make the world a better place, check out his Patreon Campaign.

1 thought on “How to read the contents of a text file into an NSString

  1. Apple’s preferred way of working is to use URLs rather than paths. The above example would work just as well by using URL methods:

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.