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
11
votes
2 answers

Swift 4 JSON decoding when type is only known at runtime

Is it possible with the Decodable protocol in Swift 4 to decode a JSON object when the type to decode to is only known at runtime? I have a registry of sorts which maps a String identifier to the type we want to decode to, as below: import…
Dave Rogers
  • 135
  • 7
11
votes
1 answer

When to use protocol in Swift

I ask a question today because I’m little bit lost today. It’s about Swift and protocol and more about Protocol Oriented Programming (POP). I read articles about it, even a book but I’m still confused. Everybody seems to say that Protocol is a great…
DEADBEEF
  • 1,930
  • 2
  • 17
  • 32
11
votes
1 answer

Swift 3 conversion Error/NSError

While trying to migrate to Swift 3 (in a project which contains about half/half swift/objective-c code), I am facing an issue. We declare this specific protocol in objective-c like this: @protocol AProtocolDeclaration -…
Anand
  • 864
  • 10
  • 31
11
votes
1 answer

Downcast Generic AnyObject to Protocol Associated Type Self.Model

I am developing a library Restofire in which i want to keep a configuration object. I want to have a ResponseSerializer in the configuration object but the thing is ResponseSerializer is a generic. public struct Configuration { /// The…
Rahul Katariya
  • 3,528
  • 3
  • 19
  • 24
11
votes
2 answers

Make property of type and also conform to protocol in Swift

I would like to make a property that is of a certain type and also conforms to a protocol, which I would have done in Objective-C like this: @property (nonatomic) UIViewController *controller; What I am looking for is to specify…
sashimiblade
  • 620
  • 7
  • 21
10
votes
2 answers

Swift: What is the difference between a typealias and an associatedtype with a value in a protocol?

In Swift, the following code compiles without issue. protocol P1 { associatedtype T = Int } protocol P2 { typealias T = Int } To me, these appear to behave almost identically. The only difference I have noticed is that there are additional…
deaton.dg
  • 1,282
  • 9
  • 21
10
votes
1 answer

Mocking a static class method in a swift unit test in a swifty way?

I'm a seasoned Objective-c programmer but I can't say the same for Swift, I'm having a hard time unit testing a class in swift without using frameworks like OCMock. The Problem: I'm integrating Firebase into a mixed Objective-C/Swift project, and…
dev_mush
  • 2,136
  • 3
  • 22
  • 38
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…
10
votes
1 answer

see why "type does not conform to protocol" in Xcode (swift)

I frequently have relatively complicated protocols with associatedType constraints, are used in generics, are used by CoreData type extensions, etc. I therefore relatively frequently get the error: Type .. does not conform to protocol .... I usually…
qqq
  • 1,360
  • 1
  • 9
  • 24
10
votes
4 answers

How Does AnyObject Conform to NSObjectProtocol?

This question was inspired by mz2's answer on the question Check for object type fails with "is not a type" error. Consider an empty Swift class: class MyClass { } Attempting to call any NSObjectProtocol methods on an instance of this class will…
JAL
  • 41,701
  • 23
  • 172
  • 300
9
votes
1 answer

Protocol conformance with implicitly unwrapped optionals

I am trying to make a Swift protocol that I can use on UILabel, UITextField, and UITextView that incorporate their text, attributedText, and font properties. However, unfortunately these three classes are inconsistent with whether they use optional…
shim
  • 9,289
  • 12
  • 69
  • 108
9
votes
3 answers

Protocol extending Encodable (or Codable) does not conform to it

I have 2 protocols, Filters and Parameters, both of which extend Encodable protocol Filters: Encodable { var page: Int { get } } protocol Parameters: Encodable { var type: String { get } var filters: Filters { get } } I create structs…
Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
9
votes
1 answer

Swift. unowned may only be applied to class and class-bound protocol types. weak works fine

Please read the question to the end as it seems to be the duplicate of many similar others but it is not. Most of the other questions use closures with let keyword to capture weak or unowned self before object init. I do not. My code: class…
iur
  • 2,056
  • 2
  • 13
  • 30
9
votes
2 answers

`ExpressibleByArrayLiteral` conformed to by class and its superclass => " is not convertible to "

I want to be able to instantiate a subclass, here named MyLabel, which is a subclass of UILabel, using array literals. I am making use of this in my framework ViewComposer which allows for creating UIViews using an array of enums attributing the…
Sajjon
  • 8,938
  • 5
  • 60
  • 94
9
votes
1 answer

Swift protocol inheritance

I have swift code: protocol ParentProtocol { // stuff } protocol ChildProtocol: ParentProtocol { // additional stuff } protocol FooProtocol { var variable: ParentProtocol? { get } } class Foo:FooProtocol { var variable:…
somedev
  • 791
  • 11
  • 29