Create a gist now

Instantly share code, notes, and snippets.

// 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
}
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
}
// 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