Move helper method

This commit is contained in:
Erik 2016-08-13 13:32:14 -04:00
parent ddd7003d89
commit 64b9774c83
No known key found for this signature in database
GPG Key ID: 4930B7C5FBC1A69D
4 changed files with 11 additions and 9 deletions

View File

@ -140,7 +140,7 @@ class SocketSideEffectTest: XCTestCase {
func testSocketDataToAnyObject() { func testSocketDataToAnyObject() {
let data = ["test", 1, 2.2, ["Hello": 2, "bob": 2.2], true, [1, 2], [1.1, 2]] as [SocketData] let data = ["test", 1, 2.2, ["Hello": 2, "bob": 2.2], true, [1, 2], [1.1, 2]] as [SocketData]
XCTAssertEqual(data.count, socket.socketDataToAnyObject(data).count) XCTAssertEqual(data.count, data.toAnyObjectArray().count)
} }
func testHandleMultipleBinaryEvent() { func testHandleMultipleBinaryEvent() {

View File

@ -36,7 +36,7 @@ public final class SocketAckEmitter : NSObject {
public func with(_ items: SocketData...) { public func with(_ items: SocketData...) {
guard ackNum != -1 else { return } guard ackNum != -1 else { return }
socket.emitAck(ackNum, with: socket.socketDataToAnyObject(items)) socket.emitAck(ackNum, with: items.toAnyObjectArray())
} }
public func with(_ items: [AnyObject]) { public func with(_ items: [AnyObject]) {

View File

@ -29,6 +29,13 @@ enum JSONError : Error {
case notNSDictionary case notNSDictionary
} }
extension Array {
/// Because Swift 3 removes a lot of implicit briding so we have to perform more casts
func toAnyObjectArray() -> [AnyObject] {
return flatMap({$0 as? AnyObject})
}
}
extension Array where Element: AnyObject { extension Array where Element: AnyObject {
func toJSON() throws -> Data { func toJSON() throws -> Data {
return try JSONSerialization.data(withJSONObject: self as NSArray, options: JSONSerialization.WritingOptions(rawValue: 0)) return try JSONSerialization.data(withJSONObject: self as NSArray, options: JSONSerialization.WritingOptions(rawValue: 0))

View File

@ -214,7 +214,7 @@ public final class SocketIOClient : NSObject, SocketEngineClient, SocketParsable
/// Send a message to the server /// Send a message to the server
public func emit(_ event: String, _ items: SocketData...) { public func emit(_ event: String, _ items: SocketData...) {
emit(event, with: socketDataToAnyObject(items)) emit(event, with: items.toAnyObjectArray())
} }
/// Same as emit, but meant for Objective-C /// Same as emit, but meant for Objective-C
@ -230,7 +230,7 @@ public final class SocketIOClient : NSObject, SocketEngineClient, SocketParsable
/// Sends a message to the server, requesting an ack. Use the onAck method of SocketAckHandler to add /// Sends a message to the server, requesting an ack. Use the onAck method of SocketAckHandler to add
/// an ack. /// an ack.
public func emitWithAck(_ event: String, _ items: SocketData...) -> OnAckCallback { public func emitWithAck(_ event: String, _ items: SocketData...) -> OnAckCallback {
return emitWithAck(event, with: socketDataToAnyObject(items)) return emitWithAck(event, with: items.toAnyObjectArray())
} }
/// Same as emitWithAck, but for Objective-C /// Same as emitWithAck, but for Objective-C
@ -411,11 +411,6 @@ public final class SocketIOClient : NSObject, SocketEngineClient, SocketParsable
handlers.removeAll(keepingCapacity: false) handlers.removeAll(keepingCapacity: false)
} }
/// Because Swift 3 removes a lot of implicit briding so we have to perform more casts
func socketDataToAnyObject(_ data: [SocketData]) -> [AnyObject] {
return data.flatMap({$0 as? AnyObject})
}
private func tryReconnect(reason: String) { private func tryReconnect(reason: String) {
guard reconnecting else { return } guard reconnecting else { return }