Trying to read JSON file in Objective-C

后端 未结 1 509
栀梦
栀梦 2021-01-15 14:45

I am trying to read in a json file in my Objective-C application, but unfortunately I am getting a RuntimeException. The exact error is:

*** Terminating app         


        
相关标签:
1条回答
  • 2021-01-15 14:52

    From your source code and your comments I assume that the application is built as a "Command Line Tool". In that case the "Target MemberShip" checkbox is actually disabled for custom resource files like your JSON file.

    As a workaround:

    • Go to the "Build Phases" of the target.
    • Click on "Add Build Phase" and select "Add Copy Files".
    • In the new build phase, click on + and select your JSON file.

    For a command line tool, it does not seem to matter which "Destination" you choose. I tested it with "Destination: Resources". The JSON file is copied in the same directory that contains the executable file. Now

    [[NSBundle mainBundle] pathForResource:@"Transactions" ofType:@"json"];
    

    should return that path and you can read the file from your application.

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