开发者

Why currentDirectoryPath return different result under IDE and without it?

开发者 https://www.devze.com 2023-03-06 05:06 出处:网络
I have a strange trouble using below code NSString * pth = [[[NSFileManager defaultManager] currentDirectoryPath] copy];

I have a strange trouble using below code

NSString * pth = [[[NSFileManager defaultManager] currentDirectoryPath] copy];

If that code runs un开发者_开发知识库der IDE it works fine, and i have pth like that

/Users/user/Desktop/iShutdown/build/Release

But if i run that code without IDE simply launching my standalone app i see in log that pth is only contains "/"

Why? how to get same result in standalone mode?


The initial working directory will depend on how the application is launched and you should never make any assumptions about what this will be. For Finder launches it will typically be / as you have seen, while for Xcode launches it will usually be the same directory as the executable (although you can change this in the settings for the executable). There are various other ways of launching an app though, e.g. from the command line, so the initial working directory can be pretty much anything the user wants it to be.

If you just want to find some specific file which resides at a specific location relative to your app, e.g. a data file that your app needs, then you should just get the path to your application bundle and use that, not the working directory.

0

精彩评论

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

关注公众号