Where to place a .txt file and read from it in a IOS project

if let path = Bundle.main.path(forResource: "README", ofType: "txt") {
  do {
    textView.text = try String(contentsOfFile: path, encoding: .utf8)
  } catch let error {
    // Handle error here
  }
}

Just drop the file anywhere into the project browser and make sure it is added to the right target.

Just to expand on the answer, you can also place them in a folder and use: + pathForResource:ofType:inDirectory:.


In Swift 3

guard let path = Bundle.main.path(forResource: "README", ofType: "txt") else {
  return
}

textView.text = try? String(contentsOfFile: path, encoding: String.Encoding.utf8)

I would recommend to use NSFileManager and drop your file anywhere in your project :

if let path = NSBundle.mainBundle().pathForResource(name, ofType: "txt"){
    let fm = NSFileManager()
    let exists = fm.fileExistsAtPath(path)
    if(exists){
        let c = fm.contentsAtPath(path)
        let cString = NSString(data: c!, encoding: NSUTF8StringEncoding)
        ret = cString as! String
    }
}

Swift 4 (thanks to @Pierre-Yves Guillemet for original)

As long as the file is in your project (and has a .txt) this will work (in this example, I assume "MyFile.txt" is a file that is in my project):

static func LoadFileAsString() -> ()
{        
    if let path = Bundle.main.path(forResource: "MyFile", ofType: "txt")
    {
        let fm = FileManager()
        let exists = fm.fileExists(atPath: path)
        if(exists){
            let content = fm.contents(atPath: path)
            let contentAsString = String(data: content!, encoding: String.Encoding.utf8)
        }
    }
}