Last active
August 25, 2021 18:08
-
-
Save erica/d300f8dac3baa7d40aadd18bd1902172 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Returns `URL?` | |
public enum SocketEndPoint: String { | |
case eventSocket = "http://nowhere.com/events" | |
case liveSocket = "http://nowhere.com/live" | |
public var url: URL? { | |
return URL(string: self.rawValue) | |
} | |
} | |
// Uses Implicitly Unwrapped Optional in initializer | |
// Will crash on malformed URL | |
public struct WebSocket { | |
public let url: URL | |
public init(url: URL!) { | |
self.url = url | |
} | |
} | |
// His initializer | |
fileprivate init() { | |
self.eventSocket = WebSocket(url: SocketEndPoint.events.url)) | |
self.liveSocket = WebSocket(url: SocketEndPoint.live.url)) | |
self.eventSocket.delegate = self | |
self.liveSocket.delegate = self | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension URL { | |
/// Non-optional initializer with better fail output | |
public init(safeString string: String) { | |
guard let instance = URL(string: string) else { | |
fatalError("Unconstructable URL: \(string)") | |
} | |
self = instance | |
} | |
} | |
// Returns `URL` | |
public enum SocketEndPoint: String { | |
case eventSocket = "http://nowhere.com/events" | |
case liveSocket = "http://nowhere.com/live" | |
public var url: URL { | |
return URL(safeString: self.rawValue) | |
} | |
} | |
// Vanilla `URL` initialization | |
public struct WebSocket { | |
public let url: URL | |
public init(url: URL) { | |
self.url = url | |
} | |
} | |
// His initializer | |
fileprivate init() { | |
self.eventSocket = WebSocket(url: SocketEndPoint.events.url)) | |
self.liveSocket = WebSocket(url: SocketEndPoint.live.url)) | |
self.eventSocket.delegate = self | |
self.liveSocket.delegate = self | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Returns `URL` with guided fail | |
public enum SocketEndPoint: String { | |
case eventSocket = "http://nowhere.com/events" | |
case liveSocket = "http://nowhere.com/live" | |
public var url: URL? { | |
guard let url = URL(string: self.rawValue) else { | |
fatalError("Unconstructable URL: \(self.rawValue)") | |
} | |
return url | |
} | |
} | |
// Uses normal initializer | |
public struct WebSocket { | |
public let url: URL | |
public init(url: URL) { | |
self.url = url | |
} | |
} | |
// His initializer | |
fileprivate init() { | |
self.eventSocket = WebSocket(url: SocketEndPoint.events.url)) | |
self.liveSocket = WebSocket(url: SocketEndPoint.live.url)) | |
self.eventSocket.delegate = self | |
self.liveSocket.delegate = self | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How about that?