Lucci
Lucci

Reputation: 3

Objective-C - pathForResource always returns nil

I want to read a .plist file from a certain directory but pathForResource always returns nil, meaning it can't find the file I'm looking for. This is what I'm using to create the path. The file is stored in the Project folder.

NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"test" ofType: @"plist"];

What am I doing wrong? And how can I access a plist file from a directory which is not part of the project? This is for a macOS command line tool.

Upvotes: 0

Views: 385

Answers (1)

Nick K9
Nick K9

Reputation: 4663

As the comments pointed out, you can't use +[NSBundle mainBundle] unless your app is structured with a .app/ bundle. If you are making a command line app (meaning a single binary), then to read in the path, you need to figure out the location of the file a different way.

When you say "The file is stored in the Project folder", I'm not quite sure what you mean. Surely you won't expect the .plist file to be hanging out in /usr/bin or wherever you end up deploying the binary! If the plist you're reading needs to ship with the app, then you should be using a bundle. If instead you generate the plist while running the app somehow (either by downloading it from the web or by building it up through storing things incrementally), then you should create a subfolder for your app in the Application Support directory which you can query for the .plist.

Once you have the .plist path, you can use +dictionaryWithContentsOfFile: to read it in.

Upvotes: 1

Related Questions