Skip to content

Instantly share code, notes, and snippets.

@chungbkhn
Last active December 7, 2018 17:22
Show Gist options
  • Save chungbkhn/11708a4b6d82158593aebd376cb721ee to your computer and use it in GitHub Desktop.
Save chungbkhn/11708a4b6d82158593aebd376cb721ee to your computer and use it in GitHub Desktop.
Nested protocol in swift
import Foundation
// Declare protocol
protocol EXModelExampleProtocol {}
// Namespace Model
enum Model {}
// Make alias for your protocol
extension Model {
typealias ExampleProtocol = EXModelExampleProtocol
}
// Use nested protocol
struct AwesomeObject: Model.ExampleProtocol {}
struct AnotherObject {
let injectedInstance: Model.ExampleProtocol
}
// Use nested protocol inside namespace
extension Model {
struct Awesome: ExampleProtocol {}
}
// Declare protocol 2
protocol EXAPIExampleProtocol {}
// Namespace API
enum API {}
// Make alias for your protocol 2
extension API {
typealias ExampleProtocol = EXAPIExampleProtocol
}
// Just use it with namespace
struct OtherObject: API.ExampleProtocol {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment