OSDN Git Service

Doutakuを導入
[kcd/KCD.git] / KCD / NotificationObserver.swift
1 //
2 //  NotificationObserver.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2018/01/28.
6 //  Copyright © 2018年 Hori,Masaki. All rights reserved.
7 //
8
9 import Foundation
10
11 /// 開放時にremoveObserver(_:)を実行してくれるクラス
12 final class NotificationObserver {
13     
14     private var tokens: [NSObjectProtocol] = []
15     
16     func addObserver(forName name: Notification.Name, object: Any?, queue: OperationQueue? = nil, using block: @escaping (Notification) -> Void) {
17         
18         tokens += [NotificationCenter.default.addObserver(forName: name, object: object, queue: queue, using: block)]
19     }
20     
21     deinit {
22         tokens.forEach(NotificationCenter.default.removeObserver)
23     }
24 }