December 2018
Intermediate to advanced
414 pages
10h 19m
English
In Swift, the simplest form of generics would be the generics in functions. You can use generics very simply, with angled brackets, as follows:
func concat<T>(a: T, b: T) -> [T] { return [a,b]}
The concat method knows nothing about the types that you are passing in, but generics gives us many guarantees over using Any:
You can also leverage protocol conformance in your generic functions, as follows:
protocol Runnable { func run()}func run<T>(runnable: T) where T: Runnable { runnable.run()}
In this case, the method that is run can ...
Read now
Unlock full access