问题
I have created a sqlite database containing data for my app, and dragged it into the supporting files group in Xcode.
When I try to access it using:
NSString *bundlePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"database.db"];
It does not read the file. However if I put in the full file path:
NSString *bundlePath = @"Users/xxxx/Apps/App/database.db";
The app works fine.
I printed out the file path from:
NSString *bundlePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"database.db"];
and it is:
/Users/xxx/Library/Application Support/iPhone Simulator/5.1/Applications/EE614EFA-BAF9-478E-xxxx-xxxx/App.app/database.db
Any ideas why?
回答1:
The best way to deal with paths is to let the API take care of the details about "appending" stuff to path strings.
NSString *path = [[NSBundle mainBundle] pathForResource:@"database"
ofType:@"db"];
回答2:
NSString *path = [[NSBundle mainBundle] pathForResource:@"database" ofType:@".SQLite"];
来源:https://stackoverflow.com/questions/11447010/resourcepath-not-working-xcode