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:

extension
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.

Swift3

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")

Objective-C

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") 

with

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