Questions tagged [swift-protocols]

Protocols specific to the Swift language

See for general information about protocols.

The official documentation for protocols in swift is located here.

As of Swift 2.0, protocols can be extended using to provide default implementations of protocols.

1132 questions
48
votes
5 answers

When to use `protocol` and `protocol: class` in Swift?

I have setup a protocol to send some information back to the previous VC. I define it like this: protocol FilterViewControllerDelegate: class { func didSearch(Parameters:[String: String]?) } But what is the difference when using: protocol…
user2636197
  • 3,982
  • 9
  • 48
  • 69
47
votes
1 answer

Swift 2 Error using mutating function in Protocol extension "Cannot use mutating member on immutable value: 'self' is immutable

Not sure what's going on here, this seems like it should be pretty straight forward. I have a protocol that mutable var, an extension with a mutating function. Things are crapping out in the testClass.testFunc, when I try and use mtkAnimQueAppend…
46
votes
3 answers

Why can't a get-only property requirement in a protocol be satisfied by a property which conforms?

Why does the following code produce an error? protocol ProtocolA { var someProperty: ProtocolB { get } } protocol ProtocolB {} class ConformsToB: ProtocolB {} class SomeClass: ProtocolA { // Type 'SomeClass' does not conform to protocol…
solidcell
  • 7,639
  • 4
  • 40
  • 59
45
votes
3 answers

what is 'where self' in protocol extension

I saw so many examples with below format extension Protocolname where Self: UIViewController What is where Self in protocol extension. I couldn't find the documentation on this.
Mini2008
  • 627
  • 1
  • 6
  • 12
39
votes
8 answers

How can I call a static function on a protocol in a generic way?

Is there a point to declaring a static function on a protocol? The client using the protocol has to call the function on a type conforming to the protocol anyway right? That breaks the idea of not having to know the type conforming to the protocol…
SirRupertIII
  • 12,324
  • 20
  • 72
  • 121
34
votes
1 answer

Difference between Swift's hash and hashValue

The Hashable protocol in Swift requires you to implement a property called hashValue: protocol Hashable : Equatable { /// Returns the hash value. The hash value is not guaranteed to be stable /// across different invocations of the same…
cfischer
  • 24,452
  • 37
  • 131
  • 214
31
votes
2 answers

SwiftUI: ViewModifier where content is an Image

I get an error "Type 'PlayButtonModifier' does not conform to protocol 'ViewModifier'" and I do not understand why and - even more important - how to do it right. I simply try to create a ViewModifier for an Image, so that I can use for example…
jboi
  • 11,324
  • 4
  • 36
  • 43
27
votes
1 answer

Swift protocol with "where Self" clause

In addition to this syntax with a protocol extension: protocol P {} extension P where Self : UIView {} ... I discovered by accident that you can use the same where clause on the protocol itself: protocol P where Self : UIView {} Notice that this…
matt
  • 515,959
  • 87
  • 875
  • 1,141
27
votes
3 answers

Type CCC doesnt conform to protocol 'NSObjectProtocol'

I don't understand why my code doesn't work. Here it is: class Test: NSURLSessionDataDelegate { func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) { if(error == nil) { …
emoleumassi
  • 4,881
  • 13
  • 67
  • 93
26
votes
2 answers

What is the in-practice difference between generic and protocol-typed function parameters?

Given a protocol without any associated types: protocol SomeProtocol { var someProperty: Int { get } } What is the difference between these two functions, in practice (meaning not "one is generic and the other is not")? Do they generate…
JHZ
  • 1,158
  • 1
  • 10
  • 15
25
votes
2 answers

How to conform to a protocol's variables' set & get?

I'm playing around with protocols and how to conform to them. protocol Human { var height: Int { get set } } struct Boy: Human { var height: Int { return 5 } // error! } I'm trying to learn different ways that I can implement set…
mfaani
  • 33,269
  • 19
  • 164
  • 293
25
votes
3 answers

Nested types inside a protocol

It is possible to have nested types declared inside protocols, like this: protocol Nested { class NameOfClass { var property: String { get set } } } Xcode says "Type not allowed here": Type 'NameOfClass' cannot be nested in…
LettersBa
  • 747
  • 1
  • 8
  • 27
24
votes
2 answers

Swift extension of a class ONLY when it conforms to a specific protocol

Hi there =) I was just faced with a design problem where I need to (essentially) do the following: I want to inject a bit of code on viewWillAppear: of any UIViewController subclass that conforms to a protocol MyProtocol. Explained in code: protocol…
23
votes
1 answer

Mark protocol method as deprecated

How do I make a protocol method appear as deprecated for someone implementing the protocol? I've tried using @available as shown below, but there is no warning shown in Xcode when implementing the protocol method. protocol TestDelegate { …
Adam Johns
  • 35,397
  • 25
  • 123
  • 176
23
votes
4 answers

A Swift protocol requirement that can only be satisfied by using a final class

I'm modeling a owner/ownee scheme on Swift: class Owner { // ... } protocol Ownee { var owner: Owner { get } } Then I have a pair of classes professor/student that adhere to the modeled types above: class Professor:…
Ernesto
  • 3,837
  • 6
  • 35
  • 56
1
2
3
75 76