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
599
votes
8 answers

How can I make a weak protocol reference in 'pure' Swift (without @objc)

weak references don't seem to work in Swift unless a protocol is declared as @objc, which I don't want in a pure Swift app. This code gives a compile error (weak cannot be applied to non-class type MyClassDelegate): class MyClass { weak var…
hnh
  • 13,957
  • 6
  • 30
  • 40
427
votes
19 answers

How does one declare optional methods in a Swift protocol?

Is it possible in Swift? If not then is there a workaround to do it?
Selvin
  • 12,333
  • 17
  • 59
  • 80
158
votes
6 answers

Protocol can only be used as a generic constraint because it has Self or associatedType requirements

I have a protocol RequestType and it has associatedType Model as below. public protocol RequestType: class { associatedtype Model var path: String { get set } } public extension RequestType { public func…
Rahul Katariya
  • 3,528
  • 3
  • 19
  • 24
152
votes
3 answers

Protocol doesn't conform to itself?

Why doesn't this Swift code compile? protocol P { } struct S: P { } let arr:[P] = [ S() ] extension Array where Element : P { func test() -> [T] { return [] } } let result : [S] = arr.test() The compiler says: "Type P does not…
matt
  • 515,959
  • 87
  • 875
  • 1,141
146
votes
8 answers

Usage of protocols as array types and function parameters in swift

I want to create a class that can store objects conforming to a certain protocol. The objects should be stored in a typed array. According to the Swift documentation protocols can be used as types:  Because it is a type, you can use a protocol in…
snod
  • 2,442
  • 3
  • 20
  • 22
134
votes
2 answers

What does "Protocol ... can only be used as a generic constraint because it has Self or associated type requirements" mean?

I am trying to create a Dictionary (actually a HashSet) keyed on a custom protocol in Swift, but it is giving me the error in the title: Protocol 'myProtocol' can only be used as a generic constraint because it has Self or associated type…
devios1
  • 36,899
  • 45
  • 162
  • 260
123
votes
3 answers

Non-'@objc' method does not satisfy optional requirement of '@objc' protocol

Overview: I have a protocol P1 which provides a default implementation of one of the Objective-C optional functions. When I provide a default implementation of the optional function there is a warning Compiler Warning: Non-'@objc' method…
user1046037
  • 16,755
  • 12
  • 92
  • 138
116
votes
8 answers

Swift - class method which must be overridden by subclass

Is there a standard way to make a "pure virtual function" in Swift, ie. one that must be overridden by every subclass, and which, if it is not, causes a compile time error?
JuJoDi
  • 14,627
  • 23
  • 80
  • 126
104
votes
5 answers

In Swift, how can I declare a variable of a specific type that conforms to one or more protocols?

In Swift I can explicitly set the type of a variable by declaring it as follows: var object: TYPE_NAME If we want to take it a step further and declare a variable that conforms to multiple protocols we can use the protocol declarative: var object:…
Daniel Galasko
  • 23,617
  • 8
  • 77
  • 97
104
votes
15 answers

How to make an enum conform to a protocol in Swift?

Swift documentation says that classes, structs, and enums can all conform to protocols, and I can get to a point where they all conform. But I can't get the enum to behave quite like the class and struct examples: protocol ExampleProtocol { var…
Adrian Harris Crowne
  • 1,335
  • 2
  • 12
  • 11
92
votes
4 answers

"fatal error: array cannot be bridged from Objective-C"—Why are you even trying, Swift?

I have declared a Swift protocol: protocol Option { var name: String { get } } I declare multiple implementations of this protocol—some classes, some enums. I have a view controller with a property declared as so: var options: [Option] =…
Robert Atkins
  • 23,528
  • 15
  • 68
  • 97
90
votes
9 answers

Protocol func returning Self

I have a protocol P that returns a copy of the object: protocol P { func copy() -> Self } and a class C that implements P: class C : P { func copy() -> Self { return C() } } However, whether I put the return value as Self I get…
aeubanks
  • 1,261
  • 1
  • 12
  • 12
89
votes
4 answers

Why I can't use let in protocol in Swift?

I have a doubt about protocols in Swift about the use of var and the keywords { get set }. From Apple documentation: If a protocol requires a property to be gettable and settable, that property requirement cannot be fulfilled by a constant stored…
Massimo Polimeni
  • 4,826
  • 4
  • 27
  • 54
81
votes
2 answers

Can I have an init func in a protocol?

When I try to implement my protocol this way: protocol Serialization { func init(key keyValue: String, jsonValue: String) } I get an error saying: Expected identifier in function declaration. Why am I getting this error?
Aaron Bratcher
  • 6,051
  • 2
  • 39
  • 70
63
votes
5 answers

Arrays of Generics in Swift

I've been playing around with arrays of generic classes with different types. It's easiest to explain my problem with some sample code: // Obviously a very pointless protocol... protocol MyProtocol { var value: Self { get } } extension Int :…
ABakerSmith
  • 22,759
  • 9
  • 68
  • 78
1
2 3
75 76