Questions tagged [protocol-extension]

90 questions
0
votes
1 answer

method in protocol extension gets called instead of method implementation in View Controller

so I have a viewController which holds a custom view, and that viewController class conforms to ViewProtocol I expect when someAction method triggered in someCustomizedView it will print " method in otherCustomizedClass called " but it prints ("…
Ian
  • 239
  • 1
  • 14
0
votes
2 answers

"self as?" within protocol extension

I was trying for casting "self" within protocol extension. It crashed with EXC_BAD_ACCESS error when running on real device but worked well on simulator. What should I do to make this work? Thanks My whole code: protocol SomeProtocol: class…
Cuong Nguyen
  • 376
  • 1
  • 5
  • 8
0
votes
2 answers

Is this a bug in Xcode or a practice should be avoid by programmer?

In the first place, I looked for a way to simplify coding by providing default argument values for protocol functions. I took the solution here and then found some fatal subsequence it may bring: protocol Foo { func foo(_ a: Int) } extension…
Nandin Borjigin
  • 2,094
  • 1
  • 16
  • 37
0
votes
1 answer

How to invoke protocol extension default implementation with type constraints

Consider the following example: class ManObj { func baseFunc() { print("ManObj baseFunc") } } class SubObj: ManObj { } protocol Model { } extension Model { // This is protocol extension func someFunc() { // Protocol extension…
ewcy
  • 323
  • 2
  • 9
0
votes
2 answers

Protocol-Oriented Programming Extension variable init twice

Here is the situation. I have a protocol, and extension of it. protocol CustomViewAddable { var aView: UIView { get } var bView: UIView { get } func setupCustomView() } extension CustomViewAddable where Self: UIViewController { var…
stan liu
  • 1,712
  • 1
  • 11
  • 14
0
votes
1 answer

Segmentation fault 11 for function naming in protocol extension

The problem is really simple. This causes a segmentation fault: extension Sequence { func parallelForEach(_ f: @escaping (T) -> R, completion: @escaping ([R]) -> ()) where Iterator.Element == T { } } Clearly I don't know how to…
0
votes
0 answers

Swift Protocol Extensions: Adding #selector for a UIViewController

I am trying to add a UITapGestureRecognizer to a UIViewController via a swift protocol extension. I run into a problem when I try intialize the tapGestureRecognizer with a #selector. I have setup the following in a playground protocol Tapable { …
Lneuner
  • 1,090
  • 1
  • 9
  • 19
0
votes
1 answer

Swift protocol extension static-methods dispatch with superclass and subclass

I'm having a similar but slightly different problem described in: Swift protocol extension method dispatch with superclass and subclass. The problem is related to static methods on protocols. I have the following code: protocol Protocol: class { …
0
votes
0 answers

Swift: extending sequences with a generic element

In Swift, one can extend a sequence that contains elements of a particular type, e.g.: extension SequenceType where Generator.Element == Rect { func intersection() -> Rect? { ... } } // when using let intersection = [rect1, rect2,…
MrMobster
  • 1,851
  • 16
  • 25
0
votes
1 answer

Adding protocol conformance to existing class then checking if implements?

I'm trying to group a couple of existing classes into a single custom protocol so I can treat them the same. For example, I'd like to group these two classes together under a single protocol like this: protocol CLKComplicationTemplateRingable { …
TruMan1
  • 33,665
  • 59
  • 184
  • 335
0
votes
1 answer

Swift 2 Protocol extensions usage

I'm trying to catch up with this protocol-oriented-programming coolness using Swift 2 but I'm currently quite lost. I'm trying to apply the theory to practical use cases so let's start with the most obvious: Let's say I have a UITextField and I want…
rmvz3
  • 1,133
  • 2
  • 16
  • 27
0
votes
0 answers

Protocol extensions - unable to find matching property

Using these protocol definitions: protocol Presenter: AnyObject { typealias InteractorType var interactor: InteractorType { get } } protocol Activable: AnyObject { var active: Bool { get set } } extension Activable where Self:…
Jakub Vano
  • 3,833
  • 15
  • 29
0
votes
1 answer

Swift 2.0: Protocol Extension Class Method returning Self

In order to extend some functionalities of my NSManagedObject subclasses, I have defined a series of protocols: protocol ManagedObjectFindable { static func find(format:String, arguments: [AnyObject]?, inContext context:NSManagedObjectContext,…
ambientlight
  • 7,212
  • 3
  • 49
  • 61
0
votes
1 answer

Stubbing methods for unit testing in swift 2

I am finding it difficult to stub methods in structs in swift. I can do it currently using a way that I'll explain below, but it feels wrong to me and would like an opinion on it. I do not use any third party libraries for stubbing, instead I prefer…
Kedar
  • 1,298
  • 10
  • 20
-1
votes
1 answer

Swift Extension computed variable not read correctly until declared in protocol

I have a protocol extension which declares and assigns a static computed variable: protocol DataType { } extension DataType { static var mocks: [Self] { [] } } Then I have another protocol named Provider which has an associatedtype…
1 2 3 4 5
6