开发者

Accessing a plist with a console application - Xcode 4

开发者 https://www.devze.com 2023-03-30 05:36 出处:网络
I\'ve created a plist (File->New File->Resource plist) file that has a dictionary that stores floating point numbers for a console application.I\'ve pretty much created this plist to store a few numbe

I've created a plist (File->New File->Resource plist) file that has a dictionary that stores floating point numbers for a console application. I've pretty much created this plist to store a few numbers to be returned by class methods, but could change so hardcoding isn't really that great of an idea. However, when I try to access the plist file, I always get "nil" for the file path and I'm not sure what I'm doing wrong.

<plist version="1.0">
<dict>
    <key>minFloatNumber</key>
    <real>520.0</real>
</dict>
</plist>

+ (double) minFloatingPointNunber
{
    NSString *path = [[NSBundle mainBundle] pathForResource: @"FloatDictionary"
                                                     ofType: @"plist"];

    NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile: path];
    [path release];

    double f = [[dict objectForKey: @"minFloatNumber"] doubleValue];
    [dict release];

  开发者_高级运维  return f;
}

I've read various articles discussing looking at the project's "Build Phases" and looking at "Copy Bundle Resources," but I don't see any of that anywhere. I'm pretty sure there has to be something wrong with my setup, but not sure what I'm missing, or the steps I need to take to remedy the situation. Thanks in advance for the tips.


The "Console Application" template will be set up to produce a single binary — not included inside a proper bundle, so you can't use the NSBundle APIs to access files. If you want to include a file, you could make an actual application (where you'll be able to use [NSBundle mainBundle]), or you could include the data inside the binary (as a compile-time constant or some such).

0

精彩评论

暂无评论...
验证码 换一张
取 消