From 8153d0e1389e31563771a07c75dfa64dd2821a18 Mon Sep 17 00:00:00 2001 From: Igor Kislyuk Date: Fri, 24 Nov 2017 09:14:46 +0300 Subject: [PATCH 1/4] Swift 4 fixes --- Source/SocketAckEmitter.swift | 2 +- Source/SocketEngine.swift | 2 +- Source/SocketIOClient.swift | 12 ++++++------ Source/SocketIOClientConfiguration.swift | 6 ++++-- Source/SocketParsable.swift | 2 +- Source/SocketStringReader.swift | 14 +++++++------- Source/WebSocket.swift | 2 +- 7 files changed, 21 insertions(+), 19 deletions(-) diff --git a/Source/SocketAckEmitter.swift b/Source/SocketAckEmitter.swift index 4af43e7..5546f1d 100644 --- a/Source/SocketAckEmitter.swift +++ b/Source/SocketAckEmitter.swift @@ -66,7 +66,7 @@ public final class OnAckCallback : NSObject { DefaultSocketLogger.Logger.log("OnAckCallback for \(ackNumber) being released", type: "OnAckCallback") } - public func timingOut(after seconds: Int, callback: @escaping AckCallback) { + @objc public func timingOut(after seconds: Int, callback: @escaping AckCallback) { guard let socket = self.socket else { return } socket.ackQueue.sync() { diff --git a/Source/SocketEngine.swift b/Source/SocketEngine.swift index 3bf4ae6..c8645f8 100644 --- a/Source/SocketEngine.swift +++ b/Source/SocketEngine.swift @@ -154,7 +154,7 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll private func handleBase64(message: String) { // binary in base64 string - let noPrefix = message[message.index(message.startIndex, offsetBy: 2).. Void)?) { + @objc public func connect(timeoutAfter: Int, withHandler handler: (() -> Void)?) { assert(timeoutAfter >= 0, "Invalid timeout: \(timeoutAfter)") guard status != .connected else { @@ -196,7 +196,7 @@ public final class SocketIOClient : NSObject, SocketEngineClient, SocketParsable } /// Disconnects the socket. - public func disconnect() { + @objc public func disconnect() { DefaultSocketLogger.Logger.log("Closing socket", type: logType) didDisconnect(reason: "Disconnect") @@ -208,7 +208,7 @@ public final class SocketIOClient : NSObject, SocketEngineClient, SocketParsable } /// Same as emit, but meant for Objective-C - public func emit(_ event: String, with items: [Any]) { + @objc public func emit(_ event: String, with items: [Any]) { guard status == .connected else { handleEvent("error", data: ["Tried emitting \(event) when not connected"], isInternalMessage: true) return @@ -224,7 +224,7 @@ public final class SocketIOClient : NSObject, SocketEngineClient, SocketParsable } /// Same as emitWithAck, but for Objective-C - public func emitWithAck(_ event: String, with items: [Any]) -> OnAckCallback { + @objc public func emitWithAck(_ event: String, with items: [Any]) -> OnAckCallback { return createOnAck([event] + items) } @@ -347,7 +347,7 @@ public final class SocketIOClient : NSObject, SocketEngineClient, SocketParsable /// Adds a handler for an event. /// Returns: A unique id for the handler @discardableResult - public func on(_ event: String, callback: @escaping NormalCallback) -> UUID { + @objc public func on(_ event: String, callback: @escaping NormalCallback) -> UUID { DefaultSocketLogger.Logger.log("Adding handler for event: %@", type: logType, args: event) let handler = SocketEventHandler(event: event, id: UUID(), callback: callback) diff --git a/Source/SocketIOClientConfiguration.swift b/Source/SocketIOClientConfiguration.swift index 4fc45ba..5a7fc07 100644 --- a/Source/SocketIOClientConfiguration.swift +++ b/Source/SocketIOClientConfiguration.swift @@ -22,7 +22,9 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -public struct SocketIOClientConfiguration : ExpressibleByArrayLiteral, Collection, MutableCollection { +import Foundation + +@objc public final class SocketIOClientConfiguration : NSObject, ExpressibleByArrayLiteral, Collection, MutableCollection { public typealias Element = SocketIOClientOption public typealias Index = Array.Index public typealias Generator = Array.Iterator @@ -82,7 +84,7 @@ public struct SocketIOClientConfiguration : ExpressibleByArrayLiteral, Collectio return backingArray.index(after: i) } - public mutating func insert(_ element: Element, replacing replace: Bool = true) { + public func insert(_ element: Element, replacing replace: Bool = true) { for i in 0.. String.Index { - currentIndex = message.characters.index(currentIndex, offsetBy: by) + currentIndex = message.index(currentIndex, offsetBy: by) return currentIndex } mutating func read(count: Int) -> String { - let readString = message[currentIndex.. String { @@ -59,15 +59,15 @@ struct SocketStringReader { guard let foundRange = substring.range(of: string) else { currentIndex = message.endIndex - return substring + return String(substring) } - advance(by: message.characters.distance(from: message.characters.startIndex, to: foundRange.lowerBound) + 1) + advance(by: message.distance(from: message.startIndex, to: foundRange.lowerBound) + 1) - return substring.substring(to: foundRange.lowerBound) + return String(substring[.. String { - return read(count: message.characters.distance(from: currentIndex, to: message.endIndex)) + return read(count: message.distance(from: currentIndex, to: message.endIndex)) } } diff --git a/Source/WebSocket.swift b/Source/WebSocket.swift index 34152af..35acb93 100644 --- a/Source/WebSocket.swift +++ b/Source/WebSocket.swift @@ -537,7 +537,7 @@ open class WebSocket : NSObject, StreamDelegate { if canDispatch { callbackQueue.async { [weak self] in guard let s = self else { return } - s.onConnect?() + s.onConnect?(()) s.delegate?.websocketDidConnect(socket: s) s.notificationCenter.post(name: NSNotification.Name(WebsocketDidConnectNotification), object: self) } From ee1dc3a7372f638b000694b57d4321096ed50435 Mon Sep 17 00:00:00 2001 From: Igor Kislyuk Date: Fri, 24 Nov 2017 09:20:21 +0300 Subject: [PATCH 2/4] Swift 4 warnings --- Source/SocketEngine.swift | 4 ++-- Source/SocketEnginePollable.swift | 4 ++-- Source/SocketExtensions.swift | 2 +- Source/WebSocket.swift | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Source/SocketEngine.swift b/Source/SocketEngine.swift index c8645f8..4e0d99c 100644 --- a/Source/SocketEngine.swift +++ b/Source/SocketEngine.swift @@ -437,13 +437,13 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll switch type { case .message: - handleMessage(String(fixedString.characters.dropFirst())) + handleMessage(String(fixedString.dropFirst())) case .noop: handleNOOP() case .pong: handlePong(with: fixedString) case .open: - handleOpen(openData: String(fixedString.characters.dropFirst())) + handleOpen(openData: String(fixedString.dropFirst())) case .close: handleClose(fixedString) default: diff --git a/Source/SocketEnginePollable.swift b/Source/SocketEnginePollable.swift index b050975..808c486 100644 --- a/Source/SocketEnginePollable.swift +++ b/Source/SocketEnginePollable.swift @@ -68,7 +68,7 @@ extension SocketEnginePollable { var postStr = "" for packet in postWait { - let len = packet.characters.count + let len = packet.count postStr += "\(len):\(packet)" } @@ -183,7 +183,7 @@ extension SocketEnginePollable { } func parsePollingMessage(_ str: String) { - guard str.characters.count != 1 else { return } + guard str.count != 1 else { return } var reader = SocketStringReader(message: str) diff --git a/Source/SocketExtensions.swift b/Source/SocketExtensions.swift index bf5280a..a360ea2 100644 --- a/Source/SocketExtensions.swift +++ b/Source/SocketExtensions.swift @@ -90,7 +90,7 @@ extension NSDictionary { } func toSocketConfiguration() -> SocketIOClientConfiguration { - var options = [] as SocketIOClientConfiguration + let options = [] as SocketIOClientConfiguration for (rawKey, value) in self { if let key = rawKey as? String, let opt = NSDictionary.keyValueToSocketIOClientOption(key: key, value: value) { diff --git a/Source/WebSocket.swift b/Source/WebSocket.swift index 35acb93..f57ec3d 100644 --- a/Source/WebSocket.swift +++ b/Source/WebSocket.swift @@ -115,7 +115,7 @@ open class WebSocket : NSObject, StreamDelegate { // MARK: - Block based API. - public var onConnect: ((Void) -> Void)? + public var onConnect: (() -> Void)? public var onDisconnect: ((NSError?) -> Void)? public var onText: ((String) -> Void)? public var onData: ((Data) -> Void)? @@ -537,7 +537,7 @@ open class WebSocket : NSObject, StreamDelegate { if canDispatch { callbackQueue.async { [weak self] in guard let s = self else { return } - s.onConnect?(()) + s.onConnect?() s.delegate?.websocketDidConnect(socket: s) s.notificationCenter.post(name: NSNotification.Name(WebsocketDidConnectNotification), object: self) } From 85063a40bd5436b0372d4d0b0b18ba84fbbde733 Mon Sep 17 00:00:00 2001 From: Igor Kislyuk Date: Fri, 24 Nov 2017 09:23:32 +0300 Subject: [PATCH 3/4] Fix api --- Source/SocketIOClient.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/SocketIOClient.swift b/Source/SocketIOClient.swift index eca9512..3b36c9e 100644 --- a/Source/SocketIOClient.swift +++ b/Source/SocketIOClient.swift @@ -70,7 +70,7 @@ public final class SocketIOClient : NSObject, SocketEngineClient, SocketParsable } /// Type safe way to create a new SocketIOClient. opts can be omitted - @objc public init(socketURL: URL, config: SocketIOClientConfiguration = []) { + public init(socketURL: URL, config: SocketIOClientConfiguration = []) { self.config = config self.socketURL = socketURL @@ -108,7 +108,7 @@ public final class SocketIOClient : NSObject, SocketEngineClient, SocketParsable /// Not so type safe way to create a SocketIOClient, meant for Objective-C compatiblity. /// If using Swift it's recommended to use `init(socketURL: NSURL, options: Set)` - public convenience init(socketURL: NSURL, config: NSDictionary?) { + @objc public convenience init(socketURL: NSURL, config: NSDictionary?) { self.init(socketURL: socketURL as URL, config: config?.toSocketConfiguration() ?? []) } From 095d716853d637debb70f5206eb02b863175f816 Mon Sep 17 00:00:00 2001 From: Igor Kislyuk Date: Fri, 24 Nov 2017 12:36:01 +0300 Subject: [PATCH 4/4] Revert changing configuration --- Source/SocketExtensions.swift | 2 +- Source/SocketIOClientConfiguration.swift | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/Source/SocketExtensions.swift b/Source/SocketExtensions.swift index a360ea2..bf5280a 100644 --- a/Source/SocketExtensions.swift +++ b/Source/SocketExtensions.swift @@ -90,7 +90,7 @@ extension NSDictionary { } func toSocketConfiguration() -> SocketIOClientConfiguration { - let options = [] as SocketIOClientConfiguration + var options = [] as SocketIOClientConfiguration for (rawKey, value) in self { if let key = rawKey as? String, let opt = NSDictionary.keyValueToSocketIOClientOption(key: key, value: value) { diff --git a/Source/SocketIOClientConfiguration.swift b/Source/SocketIOClientConfiguration.swift index 5a7fc07..4fc45ba 100644 --- a/Source/SocketIOClientConfiguration.swift +++ b/Source/SocketIOClientConfiguration.swift @@ -22,9 +22,7 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -import Foundation - -@objc public final class SocketIOClientConfiguration : NSObject, ExpressibleByArrayLiteral, Collection, MutableCollection { +public struct SocketIOClientConfiguration : ExpressibleByArrayLiteral, Collection, MutableCollection { public typealias Element = SocketIOClientOption public typealias Index = Array.Index public typealias Generator = Array.Iterator @@ -84,7 +82,7 @@ import Foundation return backingArray.index(after: i) } - public func insert(_ element: Element, replacing replace: Bool = true) { + public mutating func insert(_ element: Element, replacing replace: Bool = true) { for i in 0..