Questions tagged [protocol-extension]
90 questions
2
votes
1 answer
Are mixed class/protocol type constraints allowed in Swift protocol extensions?
Is it possible, in any version of Swift, to extend a protocol with mixed class/protocol type constraints? For example, I would like to extend Protocol2 only when Self is a subclass of UIViewController and conforms to Protocol1.
protocol Protocol1…

Andrew Johnson
- 579
- 4
- 23
2
votes
1 answer
"Does not conform to protocol" error when extending different class
I'm attempting to test my own class by injecting objects that adapt the URLSession and URLSessionDataTask protocols. I'm extending NSURLSession and NSURLSessionDataTask to adopt those protocols so that I can use the existing objects normally, but…

kubi
- 48,104
- 19
- 94
- 118
1
vote
1 answer
Async/await not supported in protocol extension?
In my iOS application, I have a protocol extension that is similar to the following:
protocol ViewControllerBase: UIViewController {
}
extension ViewControllerBase {
func showItem(itemID: Int) {
Task {
await…

Greg Brown
- 3,168
- 1
- 27
- 37
1
vote
1 answer
Computed property to sum numeric arrays in Swift
Task from book says: without calling reduce(_: _:) method, sum sequences of numbers by adding a computed property called sum. You should be able to use it like this:
[3, 7, 7].sum // 17
[8.5, 1.1, 0.1].sum // 9.7
Hint by authors: in…

mairo
- 155
- 8
1
vote
1 answer
How do I unit test Protocol and it's extension
I have a protocol and I have it's definition in extension.
I'd like to unit test the definition.
I googled but I couldn't find anything relevant.
The closest one I get is Mocking with Protocol or POP etc..
If someone could explain me with a sample ,…

Amogam
- 321
- 5
- 20
1
vote
1 answer
Which is an appropriate protocol constraint to identify a property as being '@Published'
I want to establish a binding for an object in a collection so that I can pass it on downstream like a reference pointer. I want this to be a convenience function but I am having some trouble.
What constraint, if any, can I put on the following…

bobby123uk
- 892
- 4
- 17
1
vote
1 answer
Non-'@objc' method 'paymentAuthorizationViewControllerDidFinish' does not satisfy requirement of '@objc' protocol
Getting error compiling
Non-'@objc' method 'paymentAuthorizationViewControllerDidFinish' does not satisfy requirement of '@objc' protocol 'PKPaymentAuthorizationViewControllerDelegate'
If i add @objc before…

Atif
- 286
- 3
- 14
1
vote
1 answer
UIImagePickerControllerDelegate didFinishPickingMediaWithInfo not called
I have a project which has many viewControllers using imagePicker. For each time, I have to copy the didFinishPickingMediaWithInfo again and only change some code.
Then I decided to wrap the UIImagePickerControllerDelegate and…

Jeff Zhang
- 140
- 1
- 7
1
vote
3 answers
understanding protocol extensions in swift
I am trying to implement a basic protocol extension like so:
protocol Value {
func get() -> Float
mutating func set(to:Float)
}
extension Value {
static func min(of a:Value, and b:Value) -> Float {
if a < b { //Expression type…

gloo
- 2,490
- 3
- 22
- 38
1
vote
1 answer
Abort trap: 6 in protocol extension with typealias definitions
I have the following code that compiles and runs in Xcode 9.2 Standard Build System and swift build -c release but it does not compile and gives abort trap: 6 when build project with New Build System (Preview) or swift build
import…

theFatih
- 21
- 6
1
vote
2 answers
Protocol Extension: cannot assign to get-only property
I am having a bit of an access control conundrum when using structs in place of classes to achieve a more protocol oriented programming approach.
I am receiving different message types over the network, which in their raw form are simply an array of…

MH175
- 2,234
- 1
- 19
- 35
1
vote
2 answers
Overriding methods in a class extension constrained to a protocol in swift
I am trying to add default implementations to UIViewController touches began to all controllers conforming to a protocol through a protocol extension. There the touch would be sent to a custom view all controllers implementing this protocol…

Jordi Serra
- 113
- 4
1
vote
1 answer
Possible Bug with Swift 3 mixing Protocols, Extensions, and Class Inheritence
I am learning all about swift, OOP, and POP. I have been mixing them together to make an abstract base class when I came across some unexpected behavior. It is best expressed in code, I will show it working as expected, and then as unexpected (to…

mogelbuster
- 1,066
- 9
- 19
1
vote
1 answer
Class isn't conforming to protocol with extension containing default implementation
I am currently working my way through he Treehouse IOS Swift course, and we are building a weather app. I've gotten to a point where I keep getting an error that my class isn't conforming to my protocol, but I can't figure out why.
Here is my…

bshock84
- 177
- 1
- 8
1
vote
2 answers
How to create a protocol extension that returns the confirming type?
I am trying to implement a protocol extension JsonProcess which has a function that takes NSData as the argument and i want to create different protocol extensions using self requirement something like this
extension JSONResource where Self: Login…

Abhinav Arora
- 11
- 2