如何在不给出绝对路径的情况下获取Xcode中的Resources文件夹的路径?

时间:2013-01-16 01:06:45

标签: objective-c xcode macos cocoa

我有这段代码:

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"]]];

我不想使用绝对路径,但当我尝试使用“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.

无论如何都不需要硬编码吗? logo.png目前位于Xcode的Resources文件夹中。

修改 我怎样才能获得目录路径?

1 个答案:

答案 0 :(得分:4)

这应该成为诀窍:

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

文档here