gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 17:04:23 28 4
gpt4 key购买 nike

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

当我给出绝对路径(例如:/Users/dw/src/Menulet)时,将文件夹复制到 NSPasteBoard 上可以工作,但当我尝试使用 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:[/Users/dw/Library/Developer/Xcode/DerivedData/Menulet-bvwpfkjlcufhxubvaxubgnubtfgi/Build/Products/Debug/Menulet.app/Contents/Resources/test] 错误

如何复制目录?

Screenshot of problem

编辑: Problem persists

最佳答案

我的猜测是,执行此操作时您缺少 '/' 字符:

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

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

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

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

更新

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

enter image description here

这样文件夹就被实际创建了,并且也被添加到了 bundle 中。

更新2

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

一旦这样做, bundle 将为您管理一切。因此只需使用以下命令搜索目录:

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

所以在你的情况下:

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

关于objective-c - 如何将主包内的目录复制到 NSPasteBoard?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14367267/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com