如何在不提供绝对路径的情况下获取 Xcode 中资源文件夹的路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14349854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:31:56  来源:igfitidea点击:

How can I get the path to my Resources folder in Xcode without giving an absolute path?

objective-cxcodemacoscocoa

提问by Nonconformist

I have this snippet of code:

我有这段代码:

NSPasteboard *pboard = [NSPasteboard pasteboardWithName:NSDragPboard];
[pboard declareTypes:[NSArray arrayWithObject:NSURLPboardType] owner:nil];
[pboard writeObjects:[NSArray arrayWithObject:[NSURL fileURLWithPath:@"/Users/dw/Desktop/macapp/Menulet/logo.png"]]];

And I don't want to use an absolute path but when I try using just "logo.png" it gives the following error:

而且我不想使用绝对路径,但是当我尝试仅使用“logo.png”时,它会出现以下错误:

NSURLs written to the pasteboard via NSPasteboardWriting must be absolute URLs.  NSURL 'logo.png -- file://localhost/Users/dw/Library/Developer/Xcode/DerivedData/Menulet-bvwpfkjlcufhxubvaxubgnubtfgi/Build/Products/Debug/' is not an absolute URL.

Is there anyway to not have to hardcode it? logo.png is currently in my Resources folder in Xcode.

无论如何不必对其进行硬编码?logo.png 目前在我的 Xcode 资源文件夹中。

EDIT:How can I get a directory path instead?

编辑:我怎样才能获得目录路径?

回答by Hyman

This should make the trick:

这应该可以解决问题:

[[NSBundle mainBundle] pathForResource:@"logo" ofType:@"png"]

Documentation here.

文档在这里