45

i want to put a .txt file in my Xcode Swift Iphone project. First, i simply drag&dropped it from my desktop to the Supporting Files Folder from the project. Isn't there a Folder like on Android "assets", so i can place my files anywhere i want?

The file in my example is called README.txt which has a bunch of lines and paragraphs.

Simple enough, now I want to print the content of the README.txt file to a view.

How do i do the read function and what path should I insert, if my file is in the project /SupportFiles/README.txt?

Thanks alot!

Felix Me
  • 481
  • 1
  • 4
  • 10
  • I think u should use a plist file makes more sense – meda Nov 29 '14 at 19:34
  • You can place the file anywhere in your project as a resource. See @Mundi answer below. If you like you can organize them in a folder name "SupportFiles". – Black Frog Nov 29 '14 at 19:42

6 Answers6

46
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:.

GGirotto
  • 848
  • 2
  • 10
  • 31
Mundi
  • 79,884
  • 17
  • 117
  • 140
  • 1
    I had some problems on Xcode itself, but after some restarts and so on it worked exactly as described. Thank you very much! Later on will try to follow the link for the folder organisation, it seems more tidy for bigger project. But for now I am happy that this little think works! (Sorry, can't vote the answer as useful, 15 Rep needed for it) – Felix Me Nov 29 '14 at 20:41
  • 1
    Thanks, please do it once you have the rep. – Mundi Nov 29 '14 at 21:16
9

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
    }
}
GPY
  • 3,832
  • 1
  • 16
  • 11
  • Why use NSFileManager, rather than doing it the way it's done in other answers? – David Findlay Jun 11 '17 at 23:00
  • 1
    Hi, I guess I am just used to work with NSFileManager as it allows other operations on the file or filesystem and handles multi-threading. Here I used it to check the path prior to access the file. I prefere to the check path first instead of catching the error from String(contentsOfFile ...) , but it is just a personal choice. – GPY Jun 13 '17 at 08:10
  • Thanks for this. I've added the Swift 4 version of this. – Aggressor Mar 01 '18 at 15:53
7

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)
        }
    }
}
Aggressor
  • 13,323
  • 24
  • 103
  • 182
4

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)
mokagio
  • 16,391
  • 3
  • 51
  • 58
2

There's a Assets.xcassets for images and files. And here's how to read it from assets.

if let data = NSDataAsset(name: "AssetName")?.data {
    textView.text = String(data: data, encoding: .utf8)
}
Kimi Chiu
  • 2,103
  • 3
  • 22
  • 37
1

If you define path's type and put ! end of the line, you won't get any warning.

 let path:String = NSBundle.mainBundle().pathForResource("README", ofType: "txt")!
 textView.text = String(contentsOfFile: path,
                 encoding: NSUTF8StringEncoding,
                 error: nil)
fatihyildizhan
  • 8,614
  • 7
  • 64
  • 88