Questions tagged [protocol-extension]

90 questions
6
votes
3 answers

Protocol extensions cannot fulfill CLLocationManagerDelegate conformance?

I'm trying to implement the CLLocationManagerDelegate protocol requirements via a protocol extension, but the location manager doesn't see it in the protocol extension and fails. However, it works with the same code when moved into the class. Here's…
TruMan1
  • 33,665
  • 59
  • 184
  • 335
6
votes
2 answers

Swift protocol extension implementing another protocol with shared associated type

Consider the following: protocol Foo { typealias A func hello() -> A } protocol FooBar: Foo { func hi() -> A } extension FooBar { func hello() -> A { return hi() } } class FooBarClass: FooBar { typealias A = String func hi() ->…
Daniel Shin
  • 5,086
  • 2
  • 30
  • 53
6
votes
3 answers

Extend UICollectionViewDataSource Protocol to add default implementations

I have a fairly big application which has a lot of collection views. Most of the collection view have same implementations for Data Source and the Flow Layout Delegate (same sizes, margins etc). I am trying to create a single protocol which provides…
6
votes
1 answer

Swift 2: UITableViewDataSource protocol extension

I have been playing around with protocol extensions and I have a problem. Maybe what I want to achieve can’t be done. I have this playground: //: Playground - noun: a place where people can play import UIKit protocol ArrayContainer { …
manueGE
  • 1,169
  • 11
  • 14
6
votes
0 answers

Multiple protocol inheritance in Swift 2.0 using extension protocols

Playing with the new extension protocols in Swift 2.0 come by this example: protocol A { func foo() } protocol B { func foo() } extension A {     func foo() { print("A") } } extension B { func foo() { print("B") } } class C: A, B…
Victor Sigler
  • 23,243
  • 14
  • 88
  • 105
4
votes
1 answer

'Equatable' cannot be automatically synthesized in an extension

This works: // Conformance to protocol correctly synthesized by compiler struct MyStruct: Equatable { } This doesn't: struct MyStruct { } // Doesn't work, even though the extension is in the same file extension MyStruct: Equatable { } The error…
R.B.
  • 422
  • 4
  • 10
4
votes
2 answers

Swift generics extension to multiple classes

So I want to add a generic extension to NSNumber,Int,Double and Float where the value is converted to a formatted String. I started by creating a custom protocol: protocol MyFormatConvertible { var toMyFormat: String { get } } extension…
Lefteris
  • 14,550
  • 2
  • 56
  • 95
4
votes
0 answers

Undefined symbols for architecture: *Extension where Self : Protocol* referenced from: protocol witness for *protocol.func*

In Framework1 I have the following code: public protocol SomeProtocol{ func doSomething() } extension SomeProtocol where Self : UIButton{ public func doSomething(){} } In Framework2, which imports Framework1 as a Cocoa pod, I add…
Luke Street
  • 441
  • 1
  • 4
  • 8
4
votes
2 answers

Why do I get the error “Protocol … can only be used as a generic constraint because it has Self or associated type requirements”?

I wrote an extension onto Int as below. extension Int { func squared () -> Int { return self * self } } print(10.squared()) // works The above code works. Now I want to extend the IntegerType protocol so that Int, UInt, Int64, etc…
mfaani
  • 33,269
  • 19
  • 164
  • 293
3
votes
3 answers

Calling a protocol extension initializer

I'm trying to build a set of classes that would share common initializing code. Apart from inheritance, I thought protocols would be the way to go. While protocols and protocol extensions worked for me with instance and static methods, I have some…
Romain
  • 3,718
  • 3
  • 32
  • 48
3
votes
3 answers

Can I force the compiler to fail when some protocol functions change their signature?

Is there a way to mark some Swift functions as implementing some protocol functions so that if the protocol's signature changes, the compiler can mark implementations as an error. For instance, consider this example where I have a default…
Mick F
  • 7,312
  • 6
  • 51
  • 98
3
votes
1 answer

Migrating class extensions inheritance clause to protocol extensions

I've had a VehicleModels framework with classes like Car, Bike, Plane. In the other framework VehicleInventory I needed to print customised descriptions (specific to the second framework) in a table. So I have added protocol DescriptableVehicle with…
Fishman
  • 1,737
  • 1
  • 25
  • 42
3
votes
3 answers

Swift Protocol Extension with AssociatedType Constrained to Collection, Can't Use Subscript

I'm trying to write a protocol that conforms to the Collection Protocol, and it has an associatedType - Object and a property object. protocol DDCDataSource: Collection { associatedtype Object var object: Object {get set} } I want to add…
3
votes
1 answer

Swift Protocol Oriented Mixed Scope

I have a Protocol Oriented Programming conceptual question. Let's say I am creating a protocol Foo, and I want to extend Foo with a function action() in a protocol extension. action() will always be mostly the same, no matter who is implementing, so…
3
votes
1 answer

Swift protocol to only implemented by specific classes

I want to create a protocol which is only adopted by a specific class and its subClassses in swift. I know i can use protocol extensions like this protocol PeopleProtocol: class { } extension PeopleProtocol where Self: People { } But…
Madu
  • 4,849
  • 9
  • 44
  • 78