NSBundle.mainBundle().pathForResource returns nil NSBundle.mainBundle().pathForResource returns nil ios ios

NSBundle.mainBundle().pathForResource returns nil

Do not include the .txt in the name parameter, pass it as the extension parameter.
From the documentation:

The filename extension of the file to locate.
If you specify an empty string or nil, the extension is assumed not to exist and the file is the first file encountered that exactly matches name.


let bundle = Bundle.mainlet path = bundle.path(forResource: "Test", ofType: "txt")

Swift1 & Swift2

let bundle = NSBundle.mainBundle()let path = self.bundle.pathForResource("Test", ofType: "txt")


NSBundle* bundle = [NSBundle mainBundle];NSString* path = [bundle pathForResource:@"Test" ofType:@"txt"];

In swift 3.0, write with

let path = Bundle.main.path(forResource: "Test", ofType: "txt")

Replace your

 let path = self.bundle.pathForResource("Test.txt", ofType: "txt") 


let path = NSBundle.mainBundle().pathForResource("Test", ofType: "txt")