Questions tagged [protocol-extension]
90 questions
1
vote
3 answers
Swift protocol extension `var { get }` override implementation's `let`
Implementing property with var and let behaves differently when the implementing struct is assigned to a variable typed as protocol.
protocol Req {
var path: String? { get }
}
extension Req {
var path: String? { return "Req" }
}
struct LetReq:…

Jechol Lee
- 145
- 10
1
vote
1 answer
Swift enum multiple generic types involving depending protocols
protocol ProtocolA {
func someFunc() -> Any
}
protocol ProtocolB: ProtocolA {
var someVar: Any { get }
}
enum MyEnum {
case A(T)
case B(U)
}
protocol DataObject {
...
}
extension DataObject where…

CodingMeSwiftly
- 3,231
- 21
- 33
1
vote
1 answer
Possible to attach logic to method dynamically?
I'd like to dynamically attach a closure to another method from the init of a class. For example, for UIViewController I'd like to add an extension so I can inject code in the viewDidLoad event. I tried something like below, but it doesn't…

TruMan1
- 33,665
- 59
- 184
- 335
1
vote
1 answer
How to call protocol extensions initializers in designated initializers?
I'm trying to inject a protocol extension initializer into an existing class's designated initializer. I don't think there's a way around it without overriding the designated initializer from the class, then call the protocol extension initializer…

TruMan1
- 33,665
- 59
- 184
- 335
1
vote
1 answer
Swift protocol extension: fatal error: can't unsafeBitCast between types of different sizes
I've defined a protocol with a protocol extension to simplify working with NSError.
protocol NSErrorConvertible: RawRepresentable {
var domain: String { get }
var localizedDescription: String { get }
}
extension NSErrorConvertible where…

David Potter
- 2,272
- 2
- 22
- 35
1
vote
1 answer
ambiguous use of setValueForKey in swift Core Data KVO in constrained protocol extension
I have the following piece of code
protocol JsonParseDescriptor {
//some required methods
func parsePrimitives() {
}
extension JsonParseDescriptor where Self: NSManagedObject {
func parsePrimitives() {
self.setValue(1, forKey: "id")…

Salman Hasrat Khan
- 1,971
- 1
- 20
- 27
1
vote
1 answer
Is it posible to create a default implementation of willSet on a class protocol
What I am trying to do is notify an object when it gets replaced as a delegate from my services object. I was wondering if there is a way to create a default implamintation of willSet so I do not have to duplicate code for each service object I…

CWineland
- 615
- 7
- 18
1
vote
1 answer
Swift 2 Protocol Extensions and Conformance for Objective-C Types
I have a setup like this:
@interface Model: NSManagedObject
...
@end
And a Swift protocol like this:
@objc protocol Syncable {
var uploadURL: String { get }
var uploadParams: [String: AnyObject]? { get }
func updateSyncState()…

Shinigami
- 2,123
- 23
- 40
0
votes
2 answers
Swift protocol extension with specific Self type
I added an extension for the UnsignedInteger protocol to add a hex method that represents the number in hex format. I also want for specific conforming structs to have default value for a parameter. What I wrote is the below.
extension…

hakuna matata
- 3,243
- 13
- 56
- 93
0
votes
2 answers
Swift: extend protocol whose associated type is an array
I am trying to create an Rx operator that works on arrays. I've tried this simple extension:
extension ObservableType where Element == Array {
func beat(_ beat: Observable) -> Observable {
let lhs =…

Yotam
- 9,789
- 13
- 47
- 68
0
votes
0 answers
Is there a way to automatically invoke a protocol method after conformance in Swift?
I am trying to invoke a method of a protocol extension if a class/struct has conformed to the protocol.
The main reason is to not write the same code everywhere to perform some simple register/deinit operations.
protocol LanguageChangeDipatcher {
…
0
votes
2 answers
Multiple classes with the same generic properties and setup, how can I reduce duplicate code?
There are a lot of questions out there that are similar, but I'm having a hard time finding something that explains exactly what I'm looking for
I have multiple Services, which handle a generic Data type. They currently all subclass an…

A O
- 5,516
- 3
- 33
- 68
0
votes
1 answer
How does one make protocols and their extensions in Swift store properties?
The following is a workaround for the "problem" that protocols and their extensions in Swift do not store properties. It seems to "work" but I am wondering what reasons people might have for avoiding it?
fileprivate var strings: [String] =…

Joseph Beuys' Mum
- 2,395
- 2
- 24
- 50
0
votes
2 answers
Swift proper use of protocol extension
I'm trying to extract some of the code base for re use purpose. My approach is using Protocol and Protocol Extension instead of general BaseClass.
I have create the below a protocol and protocol extension
protocol MovieDisplay {
var…

Lê Khánh Vinh
- 2,591
- 5
- 31
- 77
0
votes
0 answers
Access protocol extensions with generics declared in a framework
Can we access protocol extensions implemented in a framework outside the framework? I mean suppose there is a target say "Framework" in which I declared a protocol and implemented protocol extension.
Now if I try to access this protocol outside the…

Ankit Sachan
- 7,690
- 16
- 63
- 98