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
9
votes
1 answer

having trouble using UIViewControllerAnimatedTransitioning with swift 3

I'm trying to follow this tutorial on building a custom transition. Once I got to a custom to the part that involves UIViewControllerAnimatedTransitioning, I began having errors. (I"m still new to swift, so it's taken a lot of effort with nothing to…
madgrand
  • 121
  • 1
  • 8
9
votes
1 answer

Function that takes a protocol and a conforming class (!) instance as parameters

I am trying to figure out how to define a function which takes the following two parameters: A protocol. An instance of a class (a reference type) conforming to that protocol. For example, given protocol P { } class C : P { } // Class, conforming…
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
9
votes
1 answer

Unexpected behaviour when extending a Swift protocol from another framework (Restofire)

I have two frameworks First - Restofire. It has a protocol ResponseSerializer with Extension. public protocol ResponseSerializable { /// The type of object returned in response. associatedtype Model /// The…
Rahul Katariya
  • 3,528
  • 3
  • 19
  • 24
9
votes
4 answers

Reference to all Swift Protocols?

Swift has a notion of class interfaces, called Protocols. However I can't seem to find a full reference of all protocols available in the Swift API. Where can I find a list of all protocols?
Bouke
  • 11,768
  • 7
  • 68
  • 102
8
votes
1 answer

How do I conform to a protocol with an actor?

When I try to define an actor that conforms to a protocol, Xcode gives me the error Actor-isolated instance method 'foo()' cannot be used to satisfy nonisolated protocol requirement. I can make the func nonisolated I don't think I want to. Do I need…
Josh Paradroid
  • 1,172
  • 18
  • 45
8
votes
2 answers

Swift: "Type 'any Hashable' cannot conform to 'Hashable'"

I'm a bit confused by an error I'm seeing in the new Xcode beta around the new any syntax in Swift. I have a view that takes an any Hashable, and I'm trying to pass that to a parameter that takes a Hashable parameter. However, I get this error from…
merithayan
  • 398
  • 5
  • 13
8
votes
2 answers

'let' property may not be initialized directly; use "self.init(...)" or "self = ..." instead

I want to try to write a default init in a protocol extension, so I did this: protocol P { var data: [AnyHashable: Any] { get } init(_ s: String) } extension P { init(_ s: String) { self.data = ["s": s] } } But I'm getting…
8
votes
2 answers

Populating objects from cloud records (or another external source) using a generic function

I'm building a generic API for my Swift applications. I use CoreData for local storage and CloudKit for cloud synchronization. in order to be able to work with my data objects in generic functions I have organized them as follows (brief…
Joris416
  • 4,751
  • 5
  • 32
  • 59
8
votes
6 answers

Creating a generic UViewController initialiser

I'm trying to create a UIViewController extension that I can use to initialise new instances. For each view controller in my project I have a corresponding storyboard. i.e. …
Ollie
  • 1,926
  • 1
  • 20
  • 35
8
votes
2 answers

Swift 3: Is there a way to cast an object to a class and protocol at the same time?

I've read through the relevant sections of Apple's Swift iBook (Type Casting & Protocols) but I can seem to find a way to specify that an object is an instance of a particular class that conforms to a specific protocol. As an example in…
theMikeSwan
  • 4,739
  • 2
  • 31
  • 44
8
votes
1 answer

Implementing a function with a default parameter defined in a protocol

Swift protocols can provide default implementations for functions and computed properties by adding extensions to them. I've done that plenty of times. It is my understanding that the default implementation is only used as a "fallback": It's…
Mischa
  • 15,816
  • 8
  • 59
  • 117
8
votes
0 answers

Make a type itself -- not its instances -- conform to a protocol

I was wondering if it is possible in Swift to make a type conform to a protocol, so that I can treat the type itself as conforming to a protocol the way one normally treats instances as conforming to a protocol. Example code: protocol P { func…
BallpointBen
  • 9,406
  • 1
  • 32
  • 62
8
votes
2 answers

Swift cast object to type and protocol at the same time

How can I cast a given object to a type and a protocol in order to call some methods that are defined as an extension For Example: extension Identifiable where Self: NSManagedObject, Self: JsonParseDescriptor { func someMethod() { } } Now I…
Salman Hasrat Khan
  • 1,971
  • 1
  • 20
  • 27
8
votes
2 answers

What is the difference between declaring a protocol @objc and having it conform to NSObjectProtocol in pure Swift?

Consider two Swift protocols: @objc protocol SomeProtocol { } protocol SomeOtherProtocol: NSObjectProtocol { } What is the difference between declaring a Swift protocol @objc or having it conform to NSObjectProtocol? I know that any protocol that…
JAL
  • 41,701
  • 23
  • 172
  • 300
8
votes
1 answer

How to make protocol associated type require protocol inheritance and not protocol adoption

In my swift project I have a case where I use protocol inheritance as follow protocol A : class{ } protocol B : A{ } What Im trying to achieve next is declaring another protocol with associated type, a type which must inherit from protocol A.…
Zell B.
  • 10,266
  • 3
  • 40
  • 49