开发者

Finding name of inserted CD in Cocoa

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] stringVal开发者_如何学编程ue]];
 }


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜