(I tagged it with generics because first I was playing with various solutions that used generics..)
Hello! I would like to understand if there's an easier way to accomplish what I want then the solution I found.
I want a function available on instances of UIView and possible subclasses. My direct approach was:
extension UIView {
func myFunc(_ arg: @escaping ((Self) -> Void)) {
print(arg)
}
}
However then I get: Value of type 'Self' has no member 'text'
UILabel().myFunc { label in
label.text = ""
}
I had to go via associated value like this:
protocol X {
associatedtype View: UIView
var __view: View { get }
}
extension UIView: X {
typealias View = UIView
var __view: UIView { self }
}
extension X {
func myFunc(_ arg: @escaping ((Self) -> Void)) {
print(arg)
}
}
// Now this compiles
UILabel().myFunc { label in
label.text = ""
}
Can someone ELI5 why I can't do the first approach and is there a "better" solution the one I Found?