NSWorkspace を使用して、現在実行中のすべてのユーザー アプリケーション/プロセスを取得しようとしています。以下のコードを実行すると、現在実行中のすべてのユーザー アプリケーションのサブセットのみが返され、それらが tableView に表示されます。しかし、タイマー行のコメントを外すと、すべてのアプリケーションが返され、表に表示されます。NSWorkspace.runningApplications への最初の呼び出しで、現在実行中のすべてのユーザー アプリケーションがすぐに取得されない理由がわかりません。ありがとうございました!
import Cocoa
class ViewController: NSViewController {
// MARK: - Properties
@IBOutlet weak var tableView: NSTableView!
var runningApplications = [NSRunningApplication]()
// MARK: - View Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
//NSTimer.scheduledTimerWithTimeInterval(2, target: self, selector: #selector(ViewController.refresh(_:)), userInfo: nil, repeats: true)
runningApplications = NSWorkspace.sharedWorkspace().runningApplications
print(runningApplications)
}
override var representedObject: AnyObject? {
didSet {
// Update the view, if already loaded.
}
}
func refresh(timer: NSTimer){
runningApplications = NSWorkspace.sharedWorkspace().runningApplications
tableView.reloadData()
}
}
// MARK: - NSTableViewDataSource
extension ViewController: NSTableViewDataSource {
func numberOfRowsInTableView(tableView: NSTableView) -> Int {
return runningApplications.count
}
}
// MARK: - NSTableViewDelegate
extension ViewController: NSTableViewDelegate {
func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView? {
let cellView: NSTableCellView = tableView.makeViewWithIdentifier(tableColumn!.identifier, owner: self) as! NSTableCellView
cellView.textField?.stringValue = runningApplications[row].localizedName!
return cellView
}
}