文件移入 Objective-C 后仍保留对文件的引用吗?
我有一个 Cocoa 应用程序,它存储对用户计算机上多媒体文件(图像、视频等)的引用。我想知道除了使用文件路径之外是否有一种方法可以获取对该文件的引用,这样如果用户将该文件移动到计算机上的其他文件夹,我仍然知道它在哪里。我当前正在存储从标准 Cocoa 打开对话传回的文件路径数组:
-(void)addMultimediaDidEnd:(NSOpenPanel*)sheet
returnCode:(int)returnCode
contextInfo:(NSString *)contextInfo
{
if(returnCode == NSOKButton) {
[sheet orderOut:nil];
[self saveFiles:[sheet filenames]];
}
}
I have a Cocoa application that stores a reference to multimedia files (images, videos, etc) on the user's computer. I'm wondering if there is a way to get a reference to that file other that using a file path so that if the user moves that file to a different folder on their computer, I would still know where it is. I'm currently storing the array of file paths that are passed back from the standard Cocoa open dialogue:
-(void)addMultimediaDidEnd:(NSOpenPanel*)sheet
returnCode:(int)returnCode
contextInfo:(NSString *)contextInfo
{
if(returnCode == NSOKButton) {
[sheet orderOut:nil];
[self saveFiles:[sheet filenames]];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 OS X 10.6 (Snow Leopard) 中,
NSURL
可以转换为文件引用 URL(使用-[NSURL fileReferenceURL]
),该文件在应用程序移动时引用文件正在运行。如果要保留此文件引用,请使用+[NSURL writeBookmarkData:toURL:options:error:]
传递使用-[NSURL bookmarkDataWithOptions:includeResourceValuesForKeys:relativeToURL:error]< 生成的书签数据/代码>。稍后可以通过
+[NSURL URLByResolvingBookmarkData:options:relativeToURL:bookmarkDataIsStale:error:]
传递从+[NSURL bookmarkDataWithContentsOfURL:error:]
返回的书签数据来解析书签。在 OS X 10.6 之前,可以通过 AliasManager(OS X 文件别名系统的 Carbon 时代接口)获得相同的功能(减去一些网络感知细节)。 Alias Manager 之上有几个 Objective-C 包装器,可以更好地从 Cocoa 中使用它。我最喜欢的是 Wolf Rentzsch 对 Chris Hanson 的
BDAlias
的补充(可在 github 上找到)。In OS X 10.6 (Snow Leopard), an
NSURL
can be converted to a file reference URL (using-[NSURL fileReferenceURL]
) which references a file across moves while your application is running. If you want to persist this file reference, use+[NSURL writeBookmarkData:toURL:options:error:]
passing the bookmark data generated with-[NSURL bookmarkDataWithOptions:includingResourceValuesForKeys:relativeToURL:error]
. The bookmark can be resolved later with+[NSURL URLByResolvingBookmarkData:options:relativeToURL:bookmarkDataIsStale:error:]
passing the bookmark data returned from+[NSURL bookmarkDataWithContentsOfURL:error:]
.Prior to OS X 10.6, the same functionality (minus some network aware niceties) is available via the AliasManager, a Carbon-era interface to the OS X file alias system. There are a couple of Objective-C wrappers on top of the Alias Manager that make using it from Cocoa much nicer. My favorite is Wolf Rentzsch's additions to Chris Hanson's
BDAlias
(available on github).以下是使用书签跨移动跟踪文件的简单示例:
来自 https://github。 com/ptrsghr/FileWatcher/blob/master/FileWatcherExample/Classes/FileWatcher.m
Here's a quick example of using bookmarks to track files across moves:
From https://github.com/ptrsghr/FileWatcher/blob/master/FileWatcherExample/Classes/FileWatcher.m