Questions tagged [protocol-extension]
90 questions
42
votes
2 answers
Trailing where clause for extension of non-generic type
I have the following code:
func registerNotification(name:String, selector:Selector)
{
NSNotificationCenter.defaultCenter().addObserver(self, selector: selector, name: name, object: nil)
}
func registerKeyboardNotifications()
{
let…

Sunkas
- 9,542
- 6
- 62
- 102
32
votes
1 answer
Swift property observer in protocol extension?
Consider the following:
protocol ViewControllable: class {
typealias VM: ViewModellable
var vm: VM! { get }
func bind()
}
extension ViewControllable {
var vm: VM! {
didSet {
bind()
}
}
}
I'm trying to observe vm property…

Daniel Shin
- 5,086
- 2
- 30
- 53
27
votes
5 answers
Swift make protocol extension a Notification observer
Let's consider the following code:
protocol A {
func doA()
}
extension A {
func registerForNotification() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardDidShow:"), name:…

Swift Hipster
- 1,604
- 3
- 16
- 24
26
votes
3 answers
Can Swift Method Defined on Extensions on Protocols Accessed in Objective-c
Is it possible to call methods defined in a protocol extension in Swift from Objective-C?
For example:
protocol Product {
var price:Int { get }
var priceString:String { get }
}
extension Product {
var priceString:String {
get {
…

Anthony Mattox
- 7,048
- 6
- 43
- 59
25
votes
2 answers
Extend existing protocols to implement another protocol with default implements
Is it possible to add protocol compliance to a different protocol by way of an extension?
For instance we would like A to comply with B:
protocol A {
var a : UIView {get}
}
protocol B {
var b : UIView {get}
}
I want to give a default…

Avba
- 14,822
- 20
- 92
- 192
24
votes
2 answers
Protocol extension on an ObjC protocol
I have an Objective-C protocol which is used by mostly objective-C objects and one or two Swift objects.
I would like to extend the protocol in Swift and add 2 functions. One to register for a notification and another to handle the notification.
If…

some_id
- 29,466
- 62
- 182
- 304
21
votes
6 answers
Method in non-final class must return `Self` to conform to protocol
When implementing a static protocol function returning Self in a protocol extension, an error appears at the implementation of the function in the extension (minimal simplified scenario shown without context):
import Foundation
protocol P {
…

Greg
- 9,068
- 6
- 49
- 91
13
votes
4 answers
Calling selector from protocol extension
I'm building simple theme engine and would like have an extension which adds UISwipeGestureRecognizer to UIViewController
Here is my code:
protocol Themeable {
func themeDidUpdate(currentTheme: Theme) -> Void
}
extension Themeable where Self:…

OgreSwamp
- 4,602
- 4
- 33
- 54
12
votes
4 answers
Adding Target-Action in Protocol Extension fails
I have a set of view controllers which will have a Menu bar button. I created a protocol for those viewControllers to adopt. Also, I've extended the protocol to add default functionalities.
My protocol looks like,
protocol…

Ramsundar Shandilya
- 453
- 3
- 13
12
votes
2 answers
How to call static methods on a protocol if they are defined in a protocol extension?
protocol Car {
static func foo()
}
struct Truck : Car {
}
extension Car {
static func foo() {
print("bar")
}
}
Car.foo() // Does not work
// Error: Car does not have a member named foo
Truck.foo() // Works
Xcode…

bogen
- 9,954
- 9
- 50
- 89
10
votes
2 answers
How to visualize Protocols and Extensions in UML?
It seems reasonable to use UML Interfaces to visualize Swift Protocols in UML. But how should I visualize an extension that provides a default implementation for a specific protocol? Should I just use a class like <>ProtocolName that…

bamboofighter
- 299
- 1
- 14
10
votes
5 answers
Extending Generic Integer Types in Swift
So I'm trying to extend Swift's integer types with a few convenient functions that I use a lot, however I'm not clear on which protocols I should be extending.
As an example, let's say I want to implement a function for clamping a value (if it's…

Haravikk
- 3,109
- 1
- 33
- 46
9
votes
3 answers
Swift protocol extension method dispatch with superclass and subclass
I found an interesting behaviour which seems like a bug...
Based on the behaviour described the following…

Ken Ko
- 1,517
- 2
- 15
- 21
9
votes
4 answers
Extend Array to conform to protocol if Element conforms to given protocol
I'd like to do something like this, but can't get the syntax right or find anywhere on the web that gives the right way to write it:
protocol JSONDecodeable {
static func withJSON(json: NSDictionary) -> Self?
}
protocol JSONCollectionElement:…

kylejs
- 1,128
- 11
- 25
6
votes
2 answers
Default associatedType using protocol extension
I have a protocol with an associatedType.
I want to give a default typealias for that type in the protocol extension. This is to be done only for classes that inherit from a particular class.
protocol Foo: class {
associatedtype Bar
func…

Ayush Goel
- 3,134
- 27
- 36