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

Declare protocol function with default argument values

I want this function be in protocol: func slideToRight(currentViewController viewController: UIViewController, completion: ((Bool)->())? = nil) { // do some stuff } But when I write such protocol: protocol SomeDelegate { func…
Roman Vygnich
  • 319
  • 1
  • 2
  • 6
21
votes
4 answers

Can I extend Tuples in Swift?

I'd like to write an extension for tuples of (e.g.) two value in Swift. For instance, I'd like to write this swap method: let t = (1, "one") let s = t.swap such that s would be of type (String, Int) with value ("one", 1). (I know I can very easily…
Jean-Philippe Pellet
  • 59,296
  • 21
  • 173
  • 234
19
votes
3 answers

Swift protocol nested in a class

I would like to nest a protocol in my class to implement the delegate pattern like so : class MyViewController : UIViewController { protocol Delegate { func eventHappened() } var delegate:MyViewController.Delegate? private…
Axel Guilmin
  • 11,454
  • 9
  • 54
  • 64
18
votes
1 answer

Make a swift protocol conform to Hashable

I'm going around in circles trying to get Hashable to work with multiple struct that conform to the same protocol. I have a protocol SomeLocation declared like this: protocol SomeLocation { var name:String { get } var coordinates:Coordinate…
Darren
  • 10,182
  • 20
  • 95
  • 162
18
votes
6 answers

IBOutlet crashing with EXC_BAD_ACCESS even though not nil

In a UIViewController (rolePageController) I configure another UIViewController (drawerController) and pass it 2 UIViews from the role page that will be part of the drawerController's configuration. As soon as the drawerController tries to access…
18
votes
3 answers

Pass data between ViewController and ContainerViewController

I'm working on an app, and need to pass data between view and containerView. I need to send data and receive data from both Views. Let me explain better: I can change the Label Master (Touch the Container Button) by protocol, but I can not change…
James
  • 1,167
  • 1
  • 13
  • 37
16
votes
1 answer

Why do Self and self sometimes refer to different types in static functions?

Recently I have been developing multiple heavily protocol-oriented application frameworks with Swift and noticed a few (seemingly) odd behaviors with static functions in protocol extensions, specifically where the extension functions are invoked…
Michael Fourre
  • 3,005
  • 3
  • 15
  • 26
15
votes
1 answer

KVObserving a protocol in Swift 4

I am struggling to use the new strongly-typed KVO syntax in Swift 4 to observe properties that are only visible through a protocol: import Cocoa @objc protocol Observable: class { var bar: Int { get } } @objc class Foo: NSObject, Observable { …
proxi
  • 1,243
  • 10
  • 18
14
votes
3 answers

Swift Public protocols with Internal functions and properties

I am wondering what the best practice is when I want some functions to be public and some to me internal when working with protocols.I am writing an AudioManager in Swift 3 wrapping AVPlayer as a framework. I want some methods to be public, so that…
Sajjon
  • 8,938
  • 5
  • 60
  • 94
14
votes
3 answers

Protocol function with generic type

I would like to create a protocol like the following: protocol Parser { func parse() -> ParserOutcome } enum ParserOutcome { case result(Result) case parser(Parser) } I want to have parsers that return either a result of a…
rid
  • 61,078
  • 31
  • 152
  • 193
14
votes
2 answers

Unable to use protocol as associatedtype in another protocol in Swift

I have a protocol, Address, which inherits from another protocol, Validator, and Address fulfills the Validator requirement in the extension. There is another protocol, FromRepresentable, which has an associatedType (ValueWrapper) requirement which…
Vishal Singh
  • 4,400
  • 4
  • 27
  • 43
13
votes
2 answers

Self, protocol extension and non-final class

I tried write a static method for UIView which instantiates view of that class from the nib. Method should be generic and work on every UIView subclasses. Also I want to save the type information – so, for example, in this code let myView =…
Alexander Doloz
  • 4,078
  • 1
  • 20
  • 36
11
votes
2 answers

Why 'there cannot be more than one conformance, even with different conditional bounds'?

I hoped that Swift gives me the ability to create an extension for type with specified conditions in where block. I imagined that I can extend the same generic type with different extensions dependent on concrete generic type value (T). But not.…
Valentyn Zakharenko
  • 2,710
  • 1
  • 21
  • 47
11
votes
1 answer

Generic Array of weak references to class bound protocol in Swift 4.1

I'm trying to create a generic WeakReference type that I can put into an array (and ultimately create a generic weak array type). So far so good, but the following code: class WeakReference { weak var element:…
FSMaxB
  • 2,280
  • 3
  • 22
  • 41
11
votes
1 answer

Swift - Protocol can only be used as a generic constraint because it has Self or associated type requirements

I'm working on an app which needs to query multiple APIs. I've come up with classes for each API provider (and in more extreme cases, a class for each specific API Endpoint). This is because each API query is expected to return a very strict type of…
Skwiggs
  • 1,348
  • 2
  • 17
  • 42
1 2
3
75 76