Search code examples
macosswiftcocoafoundationnspathcontrol

How to set the URL of a NSPathComponentCell?


I have a couple of NSPathControls in my settings screen. I want to be able to retrieve stored settings from NSUserDefaults and and initialise the NSPathControl's NSPathComponentCell with the stored URL.

Currently I have the following code:

class SettingsViewController: NSViewController {

    let userDefaults = NSUserDefaults.standardUserDefaults()

    var designFolderPath:String = String()
    var developmentFolderPath:String = String()

    @IBOutlet weak var designFolder: NSPathControl!
    @IBOutlet weak var developmentFolder: NSPathControl!

    @IBAction func saveUserSettings(sender: AnyObject) {
        saveUserSettings()
    }

    func saveUserSettings() {
        designFolderPath = designFolder.URL!.absoluteString
        developmentFolderPath = developmentFolder.URL!.absoluteString


        userDefaults.setObject(designFolderPath, forKey: "designFolder")
        userDefaults.setObject(developmentFolderPath, forKey: "developmentFolder")

        dismissController(self)

    }


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do view setup here.

//        let designFolderURL:NSURL = NSURL(fileURLWithPath: designFolderPath, isDirectory: true)
//        designFolder.setPathComponentCells(NSPathComponentCell.URL(designFolderURL, resourceDataDidBecomeAvailable: <#T##NSData#>))

    }

    override func viewDidDisappear() {

    }

}

In the commented out lines in the viewDidLoad() method I am trying to initialise the NSPathComponentCell with a URL, however I am not sure what to pass as NSData? Is this the right way to do this?


Solution

    • If you want to display a valid URL, just set the URL property of the NSPathControl instance with an NSURL object
    • If you want to display relative paths or other invalid URLs, look at the Apple sample code NSPathControl Basics