SWIFT: NSURL 始终为 Nil 或不可用
SWIFT: NSURL Always either Nil or unusable
所以在我的 Swift 应用程序中,我有一个选择 folder/Drive 的函数 - 然后我想将 NSURL 用作字符串。但是-当我打印 NSURL 时,它显示为 "Optional(file:///(stuff goes here)" 但是当我将它设为 String 时,它显示为 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!
如有任何帮助,我们将不胜感激。昨晚我花了几个小时试图让控制台输出以字符串形式返回,试图将 NSURL 转换为字符串,试图删除整个应用程序...
NSOpenPanel
returns 始终是 url 与 file://
方案,您可以获得其 path
属性[ 的字符串表示形式=14=]
@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!)")
}
}
}
所以在我的 Swift 应用程序中,我有一个选择 folder/Drive 的函数 - 然后我想将 NSURL 用作字符串。但是-当我打印 NSURL 时,它显示为 "Optional(file:///(stuff goes here)" 但是当我将它设为 String 时,它显示为 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!
如有任何帮助,我们将不胜感激。昨晚我花了几个小时试图让控制台输出以字符串形式返回,试图将 NSURL 转换为字符串,试图删除整个应用程序...
NSOpenPanel
returns 始终是 url 与 file://
方案,您可以获得其 path
属性[ 的字符串表示形式=14=]
@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!)")
}
}
}