title: Swift: how to stop exposing the delegate methods
body: <p><strong>WorkAround:</strong></p>
  
  <p>I am creating a utility class let's say <code>BLEScanManager</code>, which is responsible to scan nearby BLE devices. The only job of the utility class is to scan BLE devices and make a list of it.</p>
  
  <p>The other classes can create an object of this <code>BLEScanManager</code> and get an array of BLE devices, like <code>[Bluetooth]</code> (here <code>Bluetooth</code> is a custom modal class).</p>
  
  <p>Now to scan BLE devices, I have created the extension of <code>BLEScanManager</code> in the same class and override its delegate methods like below:</p>
  
  <pre><code>import UIKit
  import CoreBluetooth
  
  protocol BLEScanDelegate {
     func reloadDeviceList()
  }
  
  internal class BLEScanManager: NSObject {
  
      private var centralManager: CBCentralManager?
      var devices : [Bluetooth] = []
      var delegate: BLEScanDelegate?
  
      override init() {
          super.init()
          centralManager = CBCentralManager(delegate: self, queue: .main)
      }
  
      // MARK:- Custom methods
      func isScanning() -&gt; Bool {
          return centralManager?.isScanning ?? false
      }
  
      func stopScanning() {
          centralManager?.stopScan()
      }
  
      func startScanning() {
          devices.removeAll()
          let options: [String: Any] = [CBCentralManagerScanOptionAllowDuplicatesKey:
              NSNumber(value: false)]
          centralManager?.scanForPeripherals(withServices: nil, options: options)
      }
  }
  
  extension BLEScanManager : CBCentralManagerDelegate {
      func centralManagerDidUpdateState(_ central: CBCentralManager) {
          switch central.state {
              case .unknown:      print("central.state is .unknown")
              case .resetting:    print("central.state is .resetting")
              case .unsupported:  print("central.state is .unsupported")
              case .unauthorized: print("central.state is .unauthorized")
              case .poweredOff:   print("central.state is .poweredOff")
              case .poweredOn:    print("central.state is .poweredOn")
                                  self.startScanning()
              @unknown default:   fatalError("unknow state")
          }
      }
  
      func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
          print("Discovered \(peripheral)")
          var bluetooth = Bluetooth()
          bluetooth.name = peripheral.name
          bluetooth.identifier = peripheral.identifier.uuidString
          bluetooth.state = peripheral.state.rawValue
          bluetooth.advertisementData = advertisementData
          let power = advertisementData[CBAdvertisementDataTxPowerLevelKey] as? Double
          let value: Double = pow(10, ((power ?? 0 - Double(truncating: RSSI))/20))
          bluetooth.signalStrength = String(describing: value.round)
  
          // Do not add duplicate device
          let fitlerArray : [Bluetooth] = devices.filter { $0.identifier ==  bluetooth.identifier}
          if fitlerArray.count == 0 {
              devices.append(bluetooth)
          }
          self.delegate?.reloadDeviceList()
      }
  }
  </code></pre>
  
  <p>The thing here is these methods are exposed in the other classes too.</p>
  
  <p>For example:</p>
  
  <p>I have created an object of <code>BLEScanManager</code> in other class <code>BLEListViewController</code> to show a list of BLE device in <code>UITableView</code>.</p>
  
  <pre><code>class BLEListViewController: UITableViewController {
       var scanManager: BLEScanManager!
  }
  </code></pre>
  
  <p>I can access <code>CBCentralManagerDelegate</code> delegate methods in <code>BLEListViewController</code> class using an object <code>scanManager</code>.</p>
  
  <p>Like below,</p>
  
  <pre><code>self.scanManager.centralManager?(&lt;#T##central: CBCentralManager##CBCentralManager#&gt;, didConnect: &lt;#T##CBPeripheral#&gt;)
  </code></pre>
  
  <p>This should expose the internal utility delegate methods to the outside world. </p>
  
  <p>Question is how to stop exposing these delegates? </p>
  
  <p>Please note that, if I use <code>internal</code> keyword it only hides that specific method. But it still allows accessing all other <code>CBCentralManagerDelegate</code> methods.</p>