gpt4 book ai didi

objective-c - 双击保存的文件启动应用程序。文件名的奇怪路径

转载 作者:行者123 更新时间:2023-12-03 17:55:51 25 4
gpt4 key购买 nike

我正在使用委托(delegate)函数 -(BOOL)application:(NSApplication *)theApplication openFile:(NSString *)fileName; 通过双击之前从应用程序本身。

我遇到的主要问题是文件的路径。如果我双击桌面上 fileName 变量中的文件,我会得到以下路径:

/Users/Mark/Library/Containers/com.Mark.myApp/Data/Desktop/theFile.ff

虽然我期待类似的事情

/Users/Mark/Desktop/theFile.ff

这就是我使用 NSOpenPanel 得到的结果

我如何管理该路径?

编辑------

当我尝试使用

取消归档文件内容时

[NSKeyedUnarchiver unarchiveObjectWithData:[NSData dataWithContentsOfURL: 文件名]];

我收到错误-[NSKeyedUnarchiver initForReadingWithData:]:数据为NULL

但是当我使用 NSOpenPanel 读取相同的文件时,它可以正常工作。

编辑添加源------------

这本质上是我用来通过双击文件来管理应用程序启动的代码。

-(BOOL)application:(NSApplication *)theApplication openFile:(NSString *)fileName{
self.openFile = [NSURL URLWithString:fileName];
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
if (self.openedFile != nil) {
id obj = [NSKeyedUnarchiver unarchiveObjectWithData:[NSData dataWithContentsOfURL:filename]];
- do something with the obj -
}

}

最佳答案

有什么问题吗?它们都指向同一个文件。第一个只是沙箱表示(使用符号链接(symbolic link),因此应用程序可以访问桌面而不会破坏沙箱)

编辑:在您的代码中,您使用 URLWithString 实际上您应该使用 fileURLWithPath。
该字符串不是 URL,而是已使用 fileURLWithPath 转换为 URL 的 POSIX 路径

关于objective-c - 双击保存的文件启动应用程序。文件名的奇怪路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13648050/

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