2012-12-21 35 views
2

我正在使用NSOpenPanel在我的应用程序上设置一个文件夹,但我得到这个文件夹的路径“file:// localhost/Users/juan/“使用NSOpenPanel获取文件夹位置删除文件:// localhost

这是我的代码:

NSOpenPanel * setDirectory = [NSOpenPanel openPanel]; 
    setDirectory.canChooseFiles=NO; 
    setDirectory.canCreateDirectories=YES; 
    setDirectory.canChooseDirectories=YES; 
    setDirectory.directoryURL=_path; 
    [setDirectory beginSheetModalForWindow:self.window completionHandler:^(NSInteger result) { 
     NSLog(@" results %ld", result); 
     if (NSFileHandlingPanelCancelButton){ 
      NSLog(@"cancel"); 

     } 
     if (NSFileHandlingPanelOKButton) { 

      self.path = [[setDirectory URLs] objectAtIndex:0]; 

     } 

    }]; 
} 

我的问题是如何或我需要做的就是绝对路径,如什么‘/用户/胡安/桌面/’无文件://本地主机。

我真的很欣赏任何指针。

+0

如果用户选择多个项目有意义,为什么只处理用户选择中的一个随机项目?如果没有意义,为什么不关掉'allowedMultipleSelection'? –

回答

3

为此,请调用NSURL对象的path方法。

+0

虽然你现在应该只使用URL。你仍然必须使用一条路径的事物数量很少而且正在减少。 –

1

我发现我使用的问题[bitmapData writeToFile:nameandPath atomically:YES];它给我一个错误。
我切换到[bitmapData writeToURL:nameandPath atomically:YES];,一切正常。

+0

区别在于'writeToFile ::'将路径作为NSString对象,而'writeToURL ::'将URL作为NSURL对象。网址是现在引用文件的首选方式。顺便说一下,这两种方法都不推荐使用;你应该改用'writeToURL:options:error:',并在错误发生时处理错误。 –