accessing a file using [NSBundle mainBundle] pathForResource: ofType:inDirectory:

前端 未结 10 1347
清酒与你
清酒与你 2020-12-02 07:56

I have a file paylines.txt added inside the folder named TextFiles which resides inside the Resources folder of my iOS project in Xcod

相关标签:
10条回答
  • 2020-12-02 08:26

    All you need to do is make sure that the checkbox shown below is checked.

    0 讨论(0)
  • 2020-12-02 08:27

    well i found out the mistake i was committing i was adding a group to the project instead of adding real directory for more instructions

    0 讨论(0)
  • 2020-12-02 08:33

    Try this, it's working for me.

    NSString *str = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"paylines.txt"];  
    

    For simulator

    NSURL *rtfUrl = [[NSBundle mainBundle] URLForResource:@"paylines" withExtension:@".txt"];
    
    0 讨论(0)
  • 2020-12-02 08:34

    Are you attempting to do this inside of an XCTest and on the verge of smashing your laptop? This is the thread for you: Why can't code inside unit tests find bundle resources?

    0 讨论(0)
  • 2020-12-02 08:36

    In case of Mac OSX,

    Go to Targets -> Build Phases click + to Copy new files build phases Select product directory and drop the file there.

    Clean and run the project.

    0 讨论(0)
  • 2020-12-02 08:39

    Select Project / Specific Folder --> Right Click --> Add Files to Project -- > Select the File you want to add.

    This worked for me.

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