所以在我的Swift App中,我有一个选择文件夹/ Drive的函数 - 然后我想将NSURL用作String。然而 - 当我打印NSURL时,它出现为“可选(文件:///(东西在这里)”但是当我把它变成一个字符串时它出现为Nil。有什么我做错了吗? 代码
@IBAction func selectDrive(sender: AnyObject) {
var openPanel = NSOpenPanel()
openPanel.allowsMultipleSelection = false
openPanel.canChooseDirectories = true
openPanel.canCreateDirectories = true
openPanel.canChooseFiles = false
openPanel.beginWithCompletionHandler { (result) -> Void in
if result == NSFileHandlingPanelOKButton {
println(openPanel.URL)
self.url = openPanel.URL!
var loc = String(contentsOfURL: self.url)
println("Location is at \(loc)")
// var str = String(system("diskutil info \(loc) | grep UUID:"))
}
}
}
我还在顶部声明了以下内容
var url = NSURL()
@IBOutlet weak var driveLabel: NSTextField!
非常感谢任何帮助。昨晚我花了几个小时尝试将控制台输出作为String返回,尝试将NSURL转换为String,尝试删除整个应用程序......
答案 0 :(得分:0)
NSOpenPanel
始终返回带有file://
方案的网址,您可以使用其path
属性获取字符串表示
@IBAction func selectDrive(sender: AnyObject) {
var openPanel = NSOpenPanel()
openPanel.allowsMultipleSelection = false
openPanel.canChooseDirectories = true
openPanel.canCreateDirectories = true
openPanel.canChooseFiles = false
openPanel.beginWithCompletionHandler { (result) -> Void in
if result == NSFileHandlingPanelOKButton {
self.url = openPanel.URL!
println("Location is at \(self.url.path!)")
}
}
}