Skip to content

Commit

Permalink
fix build on xcode 8.0 with swift 3.0
Browse files Browse the repository at this point in the history
error:
1.Function types cannot have argument label 'xxx'; use '_' instead
for typealias rename xxx to _xxx

2.'ErrorProtocol' has been renamed to 'Error'
rename ErrorProtocol to Error and rename inner Error to BKError

3.Method 'xxx' with Objective-C selector 'xxx' conflicts with previous declaration with the same Objective-C selector
add @nonobjc on this method
  • Loading branch information
zhouliangshun committed Sep 24, 2016
1 parent f3423b3 commit b74d647
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 86 deletions.
22 changes: 11 additions & 11 deletions Source/BKAvailability.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,27 +82,27 @@ public enum BKUnavailabilityCause: NilLiteralConvertible {
case poweredOff

public init(nilLiteral: Void) {
self = any
self = .any
}

@available(iOS 10.0, *)
internal init(centralManagerState: CBManagerState) {
switch centralManagerState {
case .poweredOff: self = poweredOff
case .resetting: self = resetting
case .unauthorized: self = unauthorized
case .unsupported: self = unsupported
case .poweredOff: self = .poweredOff
case .resetting: self = .resetting
case .unauthorized: self = .unauthorized
case .unsupported: self = .unsupported
default: self = nil
}
}

@available(iOS 10.0, *)
internal init(peripheralManagerState: CBManagerState) {
switch peripheralManagerState {
case .poweredOff: self = poweredOff
case .resetting: self = resetting
case .unauthorized: self = unauthorized
case .unsupported: self = unsupported
case .poweredOff: self = .poweredOff
case .resetting: self = .resetting
case .unauthorized: self = .unauthorized
case .unsupported: self = .unsupported
default: self = nil
}
}
Expand Down Expand Up @@ -135,7 +135,7 @@ public extension BKAvailabilityObservable {
- parameter availabilityObserver: The availability observer to add.
*/
func addAvailabilityObserver(_ availabilityObserver: BKAvailabilityObserver) {
if !availabilityObservers.contains({ $0.availabilityObserver === availabilityObserver }) {
if !availabilityObservers.contains(where: { $0.availabilityObserver === availabilityObserver }) {
availabilityObservers.append(BKWeakAvailabilityObserver(availabilityObserver: availabilityObserver))
}
}
Expand All @@ -145,7 +145,7 @@ public extension BKAvailabilityObservable {
- parameter availabilityObserver: The availability observer to remove.
*/
func removeAvailabilityObserver(_ availabilityObserver: BKAvailabilityObserver) {
if availabilityObservers.contains({ $0.availabilityObserver === availabilityObserver }) {
if availabilityObservers.contains(where: { $0.availabilityObserver === availabilityObserver }) {
availabilityObservers.remove(at: availabilityObservers.index(where: { $0 === availabilityObserver })!)
}
}
Expand Down
2 changes: 2 additions & 0 deletions Source/BKCBPeripheralDelegateProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,12 @@ internal class BKCBPeripheralDelegateProxy: NSObject, CBPeripheralDelegate {
// print("peripheral: \(peripheral) didDiscoverDescriptorsForCharacteristic: \(characteristic), error: \(error)")
}

@nonobjc
internal func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor descriptor: CBDescriptor, error: NSError?) {
// print("peripheral: \(peripheral) didUpdateValueForDescriptor: \(descriptor), error: \(error)")
}

@nonobjc
internal func peripheral(_ peripheral: CBPeripheral, didWriteValueFor descriptor: CBDescriptor, error: NSError?) {
// print("peripheral: \(peripheral) didWriteValueForDescriptor: \(descriptor), error: \(error)")
}
Expand Down
36 changes: 19 additions & 17 deletions Source/BKCentral.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ public class BKCentral: BKPeer, BKCBCentralManagerStateDelegate, BKConnectionPoo

// MARK: Type Aliases

public typealias ScanProgressHandler = ((newDiscoveries: [BKDiscovery]) -> Void)
public typealias ScanCompletionHandler = ((result: [BKDiscovery]?, error: BKError?) -> Void)
public typealias ContinuousScanChangeHandler = ((changes: [BKDiscoveriesChange], discoveries: [BKDiscovery]) -> Void)
public typealias ContinuousScanStateHandler = ((newState: ContinuousScanState) -> Void)
public typealias ContinuousScanErrorHandler = ((error: BKError) -> Void)
public typealias ConnectCompletionHandler = ((remotePeripheral: BKRemotePeripheral, error: BKError?) -> Void)
public typealias ScanProgressHandler = ((_ newDiscoveries: [BKDiscovery]) -> Void)
public typealias ScanCompletionHandler = ((_ result: [BKDiscovery]?, _ error: BKError?) -> Void)
public typealias ContinuousScanChangeHandler = ((_ changes: [BKDiscoveriesChange], _ discoveries: [BKDiscovery]) -> Void)
public typealias ContinuousScanStateHandler = ((_ newState: ContinuousScanState) -> Void)
public typealias ContinuousScanErrorHandler = ((_ error: BKError) -> Void)
public typealias ConnectCompletionHandler = ((_ remotePeripheral: BKRemotePeripheral, _ error: BKError?) -> Void)

// MARK: Enums

Expand Down Expand Up @@ -167,10 +167,10 @@ public class BKCentral: BKPeer, BKCBCentralManagerStateDelegate, BKConnectionPoo
} else {
returnError = .internalError(underlyingError: error)
}
completionHandler?(result: result, error: returnError)
completionHandler?(result, returnError)
}
} catch let error {
completionHandler?(result: nil, error: .internalError(underlyingError: error))
completionHandler?(nil, .internalError(underlyingError: error))
return
}
}
Expand All @@ -184,19 +184,19 @@ public class BKCentral: BKPeer, BKCBCentralManagerStateDelegate, BKConnectionPoo
- parameter errorHandler: An error handler allowing you to react when an error occurs. For now this is also called when the scan is manually interrupted.
*/
@available(iOS 10.0, *)
public func scanContinuouslyWithChangeHandler(_ changeHandler: ContinuousScanChangeHandler, stateHandler: ContinuousScanStateHandler?, duration: TimeInterval = 3, inBetweenDelay: TimeInterval = 3, errorHandler: ContinuousScanErrorHandler?) {
public func scanContinuouslyWithChangeHandler(_ changeHandler: @escaping ContinuousScanChangeHandler, stateHandler: ContinuousScanStateHandler?, duration: TimeInterval = 3, inBetweenDelay: TimeInterval = 3, errorHandler: ContinuousScanErrorHandler?) {
do {
try stateMachine.handleEvent(.scan)
continuousScanner.scanContinuouslyWithChangeHandler(changeHandler, stateHandler: { newState in
if newState == .stopped && self.availability == .available {
_ = try? self.stateMachine.handleEvent(.setAvailable)
}
stateHandler?(newState: newState)
stateHandler?(newState)
}, duration: duration, inBetweenDelay: inBetweenDelay, errorHandler: { error in
errorHandler?(error: .internalError(underlyingError: error))
errorHandler?(.internalError(underlyingError: error))
})
} catch let error {
errorHandler?(error: .internalError(underlyingError: error))
errorHandler?(.internalError(underlyingError: error))
}
}

Expand All @@ -214,20 +214,20 @@ public class BKCentral: BKPeer, BKCBCentralManagerStateDelegate, BKConnectionPoo
- parameter remotePeripheral: The remote peripheral to connect to.
- parameter completionHandler: A completion handler allowing you to react when the connection attempt succeeded or failed.
*/
public func connect(_ timeout: TimeInterval = 3, remotePeripheral: BKRemotePeripheral, completionHandler: ConnectCompletionHandler) {
public func connect(_ timeout: TimeInterval = 3, remotePeripheral: BKRemotePeripheral, completionHandler: @escaping ConnectCompletionHandler) {
do {
try stateMachine.handleEvent(.connect)
try connectionPool.connectWithTimeout(timeout, remotePeripheral: remotePeripheral) { remotePeripheral, error in
var returnError: BKError?
if error == nil {
_ = try? self.stateMachine.handleEvent(.setAvailable)
} else {
returnError = .internalError(underlyingError: error)
returnError = .internalError(underlyingError: error as! Error?)
}
completionHandler(remotePeripheral: remotePeripheral, error: returnError)
completionHandler(remotePeripheral, returnError)
}
} catch let error {
completionHandler(remotePeripheral: remotePeripheral, error: .internalError(underlyingError: error))
completionHandler(remotePeripheral, .internalError(underlyingError: error))
return
}
}
Expand Down Expand Up @@ -278,7 +278,9 @@ public class BKCentral: BKPeer, BKCBCentralManagerStateDelegate, BKConnectionPoo
}

internal override func sendData(_ data: Data, toRemotePeer remotePeer: BKRemotePeer) -> Bool {
guard let remotePeripheral = remotePeer as? BKRemotePeripheral, peripheral = remotePeripheral.peripheral, characteristic = remotePeripheral.characteristicData else {
guard let remotePeripheral = remotePeer as? BKRemotePeripheral,
let peripheral = remotePeripheral.peripheral,
let characteristic = remotePeripheral.characteristicData else {
return false
}
peripheral.writeValue(data, for: characteristic, type: .withoutResponse)
Expand Down
14 changes: 7 additions & 7 deletions Source/BKCentralStateMachine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ internal class BKCentralStateMachine {

// MARK: Enums

internal enum Error: ErrorProtocol {
internal enum BKError: Error {
case transitioning(currentState: State, validStates: [State])
}

Expand Down Expand Up @@ -74,14 +74,14 @@ internal class BKCentralStateMachine {
case .initialized:
state = .starting
default:
throw Error.transitioning(currentState: state, validStates: [ .initialized ])
throw BKError.transitioning(currentState: state, validStates: [ .initialized ])
}
}

private func handleSetAvailableEvent(_ event: Event) throws {
switch state {
case .initialized:
throw Error.transitioning(currentState: state, validStates: [ .starting, .available, .unavailable(cause: nil) ])
throw BKError.transitioning(currentState: state, validStates: [ .starting, .available, .unavailable(cause: nil) ])
default:
state = .available
}
Expand All @@ -90,7 +90,7 @@ internal class BKCentralStateMachine {
private func handleSetUnavailableEvent(_ event: Event, cause: BKUnavailabilityCause) throws {
switch state {
case .initialized:
throw Error.transitioning(currentState: state, validStates: [ .starting, .available, .unavailable(cause: nil) ])
throw BKError.transitioning(currentState: state, validStates: [ .starting, .available, .unavailable(cause: nil) ])
default:
state = .unavailable(cause: cause)
}
Expand All @@ -101,7 +101,7 @@ internal class BKCentralStateMachine {
case .available:
state = .scanning
default:
throw Error.transitioning(currentState: state, validStates: [ .available ])
throw BKError.transitioning(currentState: state, validStates: [ .available ])
}
}

Expand All @@ -110,14 +110,14 @@ internal class BKCentralStateMachine {
case .available, .scanning:
break
default:
throw Error.transitioning(currentState: state, validStates: [ .available, .scanning ])
throw BKError.transitioning(currentState: state, validStates: [ .available, .scanning ])
}
}

private func handleStopEvent(_ event: Event) throws {
switch state {
case .initialized:
throw Error.transitioning(currentState: state, validStates: [ .starting, .unavailable(cause: nil), .available, .scanning ])
throw BKError.transitioning(currentState: state, validStates: [ .starting, .unavailable(cause: nil), .available, .scanning ])
default:
state = .initialized
}
Expand Down
4 changes: 2 additions & 2 deletions Source/BKConnectionAttempt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ internal class BKConnectionAttempt: Equatable {

internal let timer: Timer
internal let remotePeripheral: BKRemotePeripheral
internal let completionHandler: ((peripheralEntity: BKRemotePeripheral, error: BKConnectionPool.Error?) -> Void)
internal let completionHandler: ((_ peripheralEntity: BKRemotePeripheral, _ error: BKConnectionPool.BKError?) -> Void)

// MARK: Initialization

internal init(remotePeripheral: BKRemotePeripheral, timer: Timer, completionHandler: ((peripheralEntity: BKRemotePeripheral, error: BKConnectionPool.Error?) -> Void)) {
internal init(remotePeripheral: BKRemotePeripheral, timer: Timer, completionHandler: @escaping ((BKRemotePeripheral, BKConnectionPool.BKError?) -> Void)) {
self.remotePeripheral = remotePeripheral
self.timer = timer
self.completionHandler = completionHandler
Expand Down
24 changes: 12 additions & 12 deletions Source/BKConnectionPool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ internal class BKConnectionPool: BKCBCentralManagerConnectionDelegate {

// MARK: Enums

internal enum Error: ErrorProtocol {
internal enum BKError: Error {
case noCentralManagerSet
case alreadyConnected
case alreadyConnecting
Expand All @@ -42,7 +42,7 @@ internal class BKConnectionPool: BKCBCentralManagerConnectionDelegate {
case noConnectionAttemptForRemotePeripheral
case noConnectionForRemotePeripheral
case timeoutElapsed
case `internal`(underlyingError: ErrorProtocol?)
case `internal`(underlyingError: Error?)
}

// MARK: Properties
Expand All @@ -54,18 +54,18 @@ internal class BKConnectionPool: BKCBCentralManagerConnectionDelegate {

// MARK: Internal Functions

internal func connectWithTimeout(_ timeout: TimeInterval, remotePeripheral: BKRemotePeripheral, completionHandler: ((peripheralEntity: BKRemotePeripheral, error: Error?) -> Void)) throws {
internal func connectWithTimeout(_ timeout: TimeInterval, remotePeripheral: BKRemotePeripheral, completionHandler: @escaping ((_ peripheralEntity: BKRemotePeripheral, _ error: Error?) -> Void)) throws {
guard centralManager != nil else {
throw Error.noCentralManagerSet
throw BKError.noCentralManagerSet
}
guard !connectedRemotePeripherals.contains(remotePeripheral) else {
throw Error.alreadyConnected
throw BKError.alreadyConnected
}
guard !connectionAttempts.map({ connectionAttempt in return connectionAttempt.remotePeripheral }).contains(remotePeripheral) else {
throw Error.alreadyConnecting
throw BKError.alreadyConnecting
}
guard remotePeripheral.peripheral != nil else {
throw Error.connectionByUUIDIsNotImplementedYet
throw BKError.connectionByUUIDIsNotImplementedYet
}
let timer = Timer.scheduledTimer(timeInterval: timeout, target: self, selector: #selector(BKConnectionPool.timerElapsed(_:)), userInfo: nil, repeats: false)
remotePeripheral.prepareForConnection()
Expand All @@ -76,15 +76,15 @@ internal class BKConnectionPool: BKCBCentralManagerConnectionDelegate {
internal func interruptConnectionAttemptForRemotePeripheral(_ remotePeripheral: BKRemotePeripheral) throws {
let connectionAttempt = connectionAttemptForRemotePeripheral(remotePeripheral)
guard connectionAttempt != nil else {
throw Error.noConnectionAttemptForRemotePeripheral
throw BKError.noConnectionAttemptForRemotePeripheral
}
failConnectionAttempt(connectionAttempt!, error: .interrupted)
}

internal func disconnectRemotePeripheral(_ remotePeripheral: BKRemotePeripheral) throws {
let connectedRemotePeripheral = connectedRemotePeripherals.filter({ $0 == remotePeripheral }).last
guard connectedRemotePeripheral != nil else {
throw Error.noConnectionForRemotePeripheral
throw BKError.noConnectionForRemotePeripheral
}
connectedRemotePeripheral?.unsubscribe()
centralManager.cancelPeripheralConnection(connectedRemotePeripheral!.peripheral!)
Expand Down Expand Up @@ -119,21 +119,21 @@ internal class BKConnectionPool: BKCBCentralManagerConnectionDelegate {
failConnectionAttempt(connectionAttemptForTimer(timer)!, error: .timeoutElapsed)
}

private func failConnectionAttempt(_ connectionAttempt: BKConnectionAttempt, error: Error) {
private func failConnectionAttempt(_ connectionAttempt: BKConnectionAttempt, error: BKError) {
connectionAttempts.remove(at: connectionAttempts.index(of: connectionAttempt)!)
connectionAttempt.timer.invalidate()
if let peripheral = connectionAttempt.remotePeripheral.peripheral {
centralManager.cancelPeripheralConnection(peripheral)
}
connectionAttempt.completionHandler(peripheralEntity: connectionAttempt.remotePeripheral, error: error)
connectionAttempt.completionHandler(connectionAttempt.remotePeripheral, error)
}

private func succeedConnectionAttempt(_ connectionAttempt: BKConnectionAttempt) {
connectionAttempt.timer.invalidate()
connectionAttempts.remove(at: connectionAttempts.index(of: connectionAttempt)!)
connectedRemotePeripherals.append(connectionAttempt.remotePeripheral)
connectionAttempt.remotePeripheral.discoverServices()
connectionAttempt.completionHandler(peripheralEntity: connectionAttempt.remotePeripheral, error: nil)
connectionAttempt.completionHandler(connectionAttempt.remotePeripheral, nil)
}

// MARK: CentralManagerConnectionDelegate
Expand Down
Loading

0 comments on commit b74d647

Please sign in to comment.