Console application - NSBundle cannot get file path

New Mac console apps. I have never done this before, but I want to code using Objective-C, so I chose "Foundation" when creating the application using Xcode.

int main(int argc, const char * argv[]) { @autoreleasepool { NSString *contentPath = [[NSBundle mainBundle] pathForResource:@"Input" ofType:@"txt"]; NSLog(@"%@",contentPath); } return 0; } 

The path is null.

I answered in the following way: https://stackoverflow.com/a/166382/2/ from the related question, but it did not fix it.

Then I looked at Getting the zero path from the NSBundle , but they had a problem in the directory (I don't have directories - I just put my file in the root of the project).

Why else can I get null for this file path?

+4
source share
2 answers

It becomes null because, as a rule, when you have an .app with associated resources, you need to add a file called Input.txt to the Supported Files folder or the Resources folder. Since you are writing a console application, I do not understand why you are doing this.

Before adding the Input.txt file to the file support:

 2013-05-09 19:23:40.228 tester[2847:303] (null) 

After adding the Input.txt file to support the files:

 2013-05-09 19:21:00.306 tester[2807:303] /Developer/Xcode/DerivedData/tester-gjrofdlcblvmplbjdvvfhieiymzh/Build/Products/Debug/tester.app/Contents/Resources/Input.txt 
+1
source

Your application does not have a package (which is a specially structured directory), so viewing package resources will not look for it.

+3
source

Source: https://habr.com/ru/post/1480022/


All Articles