在 Cocoa 中查找插入的 CD 的名称

发布于 2024-09-04 03:21:16 字数 763 浏览 9 评论 0原文

我有一个书签,其中描述了如何执行此操作的过程 - 在 OS X 中查找已安装 CD 的名称 - 但当我重新格式化我的 Mac 时,我删除了该书签。 :P

阅读这个主题,我认为这可能有效。基本上,我需要在继续应用程序

  1. 访问 NSWorkspace
  2. 之前验证是否已安装特定 CD执行“checkForRemovableMedia”
  3. 从“mountedRemoveableMedia”获取已安装媒体路径数组
  4. 运行已安装媒体路径数组以查找包含目标光盘名称的路径

无论如何,这就是我想出的一个可能的解决方案。还有人在 Cocoa 这方面有任何其他想法/知识吗?建议:)

编辑: 我在下面编写了这段代码,但不起作用。它创建了一个包含 NSCFStrings 的 NSCFArray,我读了它,但不应该这样做。

 NSArray *mountedItems = [[NSWorkspace sharedWorkspace] mountedRemovableMedia];

 int count = [mountedItems count];
 int i = 0;

    for (i = 0; i < count; i++) {
         //line is not printing.  contains NSCFArray and NSCFStrings
            [NSLog print:[[mountedItems objectAtIndex:i] stringValue]];
 }

I had a bookmark which described the process on how to do this - finding the name of a mounted CD in OS X - but I deleted the bookmark when I reformatted my Mac. :P

Reading up on the subject, this is what I think might work. Basically, I need to verify if a particular CD is mounted before continuing in the application

  1. Access NSWorkspace
  2. Perform 'checkForRemovableMedia'
  3. Grab array of mounted media paths from 'mountedRemoveableMedia'
  4. Run through array of mounted media paths to find one containing name of targeted disc

Anyway, this is what I've came up with as a possible solution. Anyone else have any other ideas/knowledge in this area in Cocoa? Suggestions :)

EDIT:
I made this code below, but isn't working. It creates an NSCFArray which contains NSCFStrings, which I read up and shouldn't be doing.

 NSArray *mountedItems = [[NSWorkspace sharedWorkspace] mountedRemovableMedia];

 int count = [mountedItems count];
 int i = 0;

    for (i = 0; i < count; i++) {
         //line is not printing.  contains NSCFArray and NSCFStrings
            [NSLog print:[[mountedItems objectAtIndex:i] stringValue]];
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱本泡沫多脆弱 2024-09-11 03:21:16

好吧,所以我是个白痴。

[[NSWorkspace sharedWorkspace] checkForRemovableMedia];
NSArray *mountedItems = [[NSWorkspace sharedWorkspace] mountedRemovableMedia];

NSUInteger count = [mountedItems count];
NSUInteger i = 0;
for (i = 0; i < count; i++) {
    NSString *tempString = [mountedItems objectAtIndex:i];
    NSLog(@"%@",tempString);
}

我不仅错误地使用了 NSLog,而且完全没有意识到也许在字符串上调用“stringValue”是多余的。以及导致代码损坏的原因。 :P

现在可以了;我还添加了“checkForRemovableMedia”作为额外的预防措施。

OK, so I'm an idiot.

[[NSWorkspace sharedWorkspace] checkForRemovableMedia];
NSArray *mountedItems = [[NSWorkspace sharedWorkspace] mountedRemovableMedia];

NSUInteger count = [mountedItems count];
NSUInteger i = 0;
for (i = 0; i < count; i++) {
    NSString *tempString = [mountedItems objectAtIndex:i];
    NSLog(@"%@",tempString);
}

I was not only using NSLog wrong, but completely didn't even realize that perhaps calling 'stringValue' on a string is redundant. And also what caused the code to break. :P

This works now; I also added 'checkForRemovableMedia' as an extra precaution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文