NSOpenPanel URL到字符串

时间:2011-10-23 15:30:47

标签: cocoa

我使用下面的代码打开文件

    NSOpenPanel * panel = [NSOpenPanel openPanel];
            [panel setCanSelectHiddenExtension:YES];
            [panel setRequiredFileType:@"scpt"];
            [panel setAllowsOtherFileTypes:NO];

            [panel
             beginSheetForDirectory:nil
             file:@"Script"
             modalForWindow:[self window]
             modalDelegate:self
             didEndSelector:@selector (openFileDidEnd:returnCode:contextInfo:)
             contextInfo:nil];

-(void)openFileDidEnd:(NSSavePanel*)panel returnCode:(int)returnCode contextInfo:(void*)contextInfo
{


if(returnCode == NSOKButton)
{

    NSString *s=[[panel URL] absoluteString];
    [NSThread detachNewThreadSelector:@selector(setFileString:) toTarget:self withObject:s ];

}
};

的值是'file://home/Users/myName/Desktop/1.scpt'

如果我打电话

bool b=[[NSFileManager defaultManager] fileExistsAtPath:@"file://home/Users/myName/Desktop/1.scpt"];

检查路径s的文件是否存在,它总是返回0

但是如果我在Finder中查看,我发现它的路径是'/Users/myName/Desktop/1.scpt'

bool b=[[NSFileManager defaultManager] fileExistsAtPath:@"/Users/myName/Desktop/1.scpt"];

将返回YES!

如何从NSOpenPanel的网址获取正确的字符串路径?

欢迎任何评论

1 个答案:

答案 0 :(得分:5)

NSString *s=[[panel URL] absoluteString];

应该是

NSString *s=[[panel URL] path];

如果您的目标是Snow Leopard或以后我建议使用

- (void)beginSheetModalForWindow:(NSWindow *)window completionHandler:(void (^)(NSInteger result))handler

因为您使用的方法已被弃用,并且使用块更容易。