首先把文件(比如本例中的testFile.txt文件)放置在resources分组下,然后代码这样写:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"testFile" ofType:@"txt"];

NSLog(@"data path: %@", filePath); 

输出的日志中你可以看到testFile.txt的路径已经获得。


再举一个例子:连接SQLITE数据库

 

NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"myData" ofType:@"sqlite"];

if (sqlite3_open([dataPath UTF8String], &db) != SQLITE_OK)
{

        sqlite3_close(db);

        NSLog(@"数据库打开失败");

}
else
{

        NSLog(@"数据库成功打开");

}