如何将主包内的目录复制到NSPasteBoard?

时间:2013-01-16 20:31:20

标签: objective-c xcode macos cocoa

我一直在尝试不同的文件路径和不同类型的组合,但基本上我要做的是复制一个名为“test”的文件夹,该文件夹位于我的资源文件夹中。

当我提供绝对路径时,将文件夹复制到NSPasteBoard上(例如:/ Users / dw / src / Menulet),但是当我尝试使用mainBundle的资源路径时它不起作用。 这是我目前的代码:

NSString *resourceFolder = [[NSURL fileURLWithPath:[[NSBundle mainBundle]
                                          resourcePath]] absoluteString];

NSPasteboard *pboard = [NSPasteboard pasteboardWithName:NSDragPboard];
[pboard declareTypes:[NSArray arrayWithObject:NSURLPboardType] owner:nil];

NSString *SDKPathString = [[NSString alloc] initWithFormat:@"%@test/",
                                                        resourceFolder];
NSURL *SDKPathURL = [[NSURL alloc] initWithString:SDKPathString];

[pboard writeObjects:[NSArray arrayWithObject:SDKPathURL]];

结果是找不到文件:

__CFPasteboardIssueSandboxExtensionForPath: error for [/Users/dw/Library/Developer/Xcode/DerivedData/Menulet-bvwpfkjlcufhxubvaxubgnubtfgi/Build/Products/Debug/Menulet.app/Contents/Resources/test]

如何复制目录?

Screenshot of problem

修改 Problem persists

1 个答案:

答案 0 :(得分:2)

我的猜测是,当你这样做时,你错过了一个'/'字符:

NSString *SDKPathString = [[NSString alloc] initWithFormat:@"%@test/",
                                                    resourceFolder];

确实,resourceFolder是一个路径,我通常使用这样的东西:

NSString* SDKPathString= [resourceFolder stringByAppendingPathComponent: @"test"];

即使在你的情况下,你可以通过在'/'之前放置"test"字符来简单地纠正错误。但我认为这更像是助记符。

更新

不是说如果你创建一个组Xcode也会创建一个文件夹。如果您想确保Xcode这样做,请使用finder创建一个文件夹并将其拖到项目中。请检查以下选项:

enter image description here

这样就实际创建了文件夹,并将其添加到了包中。

更新2

您不应该尝试将项目文件夹中的目录移动到正确的位置,而是将其放在捆绑包中的任何位置,前提是该目录已复制到捆绑包中。

一旦这样做,捆绑包将为您管理一切。所以只需使用以下方法搜索目录:

- (NSString *)pathForResource:(NSString *)name ofType:(NSString *)extension;

所以在你的情况下:

NSBundle* bundle= [NSBundle mainBundle];
NSString *SDKPathString = [ bundle pathForResource: @"test" ofType: @""];