OSDN Git Service

ApplicationDirecroriesの中のアプリケーションに依存する部分を分離した
[kcd/KCD.git] / KCD / ScreenshotDetailViewController.swift
index 81508a8..3bbd1ed 100644 (file)
@@ -10,12 +10,7 @@ import Cocoa
 
 final class ScreenshotDetailViewController: BridgeViewController {
     
-    deinit {
-        
-        arrayController.removeObserver(self, forKeyPath: NSBindingName.selectionIndexes.rawValue)
-    }
-    
-    @IBOutlet var imageView: ImageView!
+    @IBOutlet private var imageView: ImageView!
     
     override var nibName: NSNib.Name {
         
@@ -29,34 +24,33 @@ final class ScreenshotDetailViewController: BridgeViewController {
     
     private var currentSelection: [ScreenshotInformation] = []
     
+    private var selectionObservation: NSKeyValueObservation?
+    
     override func viewDidLoad() {
         
         super.viewDidLoad()
         
-        arrayController.addObserver(self, forKeyPath: NSBindingName.selectionIndexes.rawValue, context: nil)
-        updateSelections()
-    }
-    
-    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) {
-        
-        if keyPath == NSBindingName.selectionIndexes.rawValue {
-            
-            updateSelections()
+        selectionObservation = arrayController.observe(\NSArrayController.selectionIndexes) { [weak self] (_, _) in
             
-            return
+            self?.updateSelections()
         }
         
-        super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
+        updateSelections()
     }
     
     private func updateSelections() {
         
-        guard let selection = arrayController.selectedObjects as? [ScreenshotInformation]
-            else { return }
+        guard let selection = arrayController.selectedObjects as? [ScreenshotInformation] else {
+            
+            return
+        }
         
-        if currentSelection == selection { return }
+        if currentSelection == selection {
+            
+            return
+        }
         
-        imageView.images = selection.flatMap { NSImage(contentsOf: $0.url) }
+        imageView.images = selection.compactMap { NSImage(contentsOf: $0.url) }
         currentSelection = selection
     }
 }