swift + OS X sandboxing: treat 'NSVBOpenPanel' as a 'NSOpenPanel' :: because I need to get the sender in the delegate method

让人想犯罪 __ 提交于 2019-12-10 21:35:13

问题


Im using swift and I show a NSOpenPanel. In the delegate I need to look at the sender's prompt to distinguish which action to take:

e.g.

func show() {
    ... 
    panel.delegate = self
    panel.prompt = "xy"

    panel.run ....
}

func show2() {
    ... 
    panel.delegate = self
    panel.prompt = "abc"

    panel.run ....
}

//delegate
func panel(sender: AnyObject, shouldEnableURL url: NSURL) -> Bool {
    let panelPrompt = (sender as! NSOpenPanel).prompt       ... 
}
  • without sandbox = WORKS fine

    • the sender of the delegate is a NSOpenPanel indeed
  • with sandbox = Cast fails, crash

    • the sender of the delegate is NOT a NSOpenPanel but a NSVBOpenPanel. Apple's private class that remotely speaks to the outside world and allows the user to choose files NORMALLY not in your sandbox. (for details I refer to apple's sandboxing guide)

So the question is how do I do use this in swift without crashing?
Is there a nice way or is it just a bug/ugly idk behavior
Do I have to revert to use performSelector?

===

Addition: extensions to NSOpenPanel don't work either!


回答1:


Instead of casting the sender to NSOpenPanel (which fails because the sender is an instance of the private NSVBOpenPanel class), or some performSelector magic, you can use the fact that arbitrary methods and properties can be accessed on AnyObject without casting, and the call behaves like an implicitly unwrapped optional:

func panel(sender: AnyObject, shouldEnableURL url: NSURL) -> Bool {
    let panelPrompt = sender.prompt ?? ""
    // ...
    return true
}

This gives the prompt for any sender object which has a prompt property, and the empty string as a fallback. In my test it worked well in a sandboxed environment.

See The strange behaviour of Swift's AnyObject for more details, examples, and references to the documentation.




回答2:


This is how it would work with performSelector. It is quite ugly though:

let panelPromptUnmanaged = (sender as! NSObject).performSelector(NSSelectorFromString("prompt"))
let panelPrompt = panelPromptUnmanaged != nil ? panelPromptUnmanaged.takeRetainedValue() as! String : ""


来源:https://stackoverflow.com/questions/37119631/swift-os-x-sandboxing-treat-nsvbopenpanel-as-a-nsopenpanel-because-i-n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!