pathForResource returns null

前端 未结 5 1337
清酒与你
清酒与你 2021-02-02 06:50

I\'m trying to load some .applescript files in my project.

Copied them into the \"Supporting Files\" directory like this:

相关标签:
5条回答
  • 2021-02-02 07:14

    In addition to what Zaph said, be sure that you are not running in 'Release' mode.

    There was a time where I forgot to change that setting back, and the symbols were stripped, thus making NSBundle incapable of resolving the path.

    0 讨论(0)
  • 2021-02-02 07:21

    For all those who are still getting a nil path and the file is added in "Copy Bundle Resources" just remove the file from Copy Bundle Resources and than add again.

    0 讨论(0)
  • 2021-02-02 07:30

    Great response from Zaph above, but I noticed when I was doing it there's quicker way, at least in Xcode 6.1.

    if you single click the file (eg. test.applescript) and have the right menu of Xcode open, you'll see a section called "Target Membership". Make sure that the right target (as in the one you're using the script with) is selected and it'll do the same job as adding the file under "Copy Bundle Resources"

    Also, not entirely relevant but in case you're loading from several different places, or you are inheriting this class. You shouldn't really use

    [NSBundle mainBundle];

    instead use

    [NSBundle bundleForClass:[self class]];
    

    That means you don't have to worry about where you are on the method call it'll take the resource from the appropriate items associated with the target you're running. Great for making libraries and reusable calls ;)

    0 讨论(0)
  • 2021-02-02 07:31

    Does your image assume that that file in some folder?

    Try pathForResource:ofType:inDirectory: method in this case:

    NSLog(@"Resource path for test.applescript: %@", [bundle pathForResource:@"test" ofType:@"applescript" inDirectory:@"folder/Supporting Files");
    
    0 讨论(0)
  • 2021-02-02 07:33

    In the Xcode target "Build Phases" add the file under "Copy Bundle Resources"".

    0 讨论(0)
提交回复
热议问题