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

How is swift allowing same method in protocol and protocol extension?

Here is the map method which seems to be duplicate in Sequence protocol. How is apple doing this? what is the intention behind this? public protocol Sequence { public func map(_ transform: (Self.Iterator.Element) throws -> T) rethrows ->…
user3869369
  • 137
  • 1
  • 11
-3
votes
1 answer

Why can Swift use { get } to declare properties in classes, shouldn't it be used in protocols?

image 1.0 I'm curious,how it can ..., and why? image 1.1 if i use { get } to declare properties in a class, it will report an error.
dyouknow
  • 1
  • 4
-3
votes
2 answers

Class does not conform to protocol with protocol extension

I do have a similar problem as protocol extension, does not conform to protocol, but the solution there does not work for me and I can not figure out why. I want to create a protocol with an extension to handle the login behaviour of my app. First…
psteinroe
  • 493
  • 1
  • 6
  • 18
-3
votes
2 answers

Having issue calling delegate method and updating UI in the previous view controller

I want to use a delegate method to update some UI in my top view controller from the current view controller. I'm getting error unexpectedly found nil while unwrapping an Optional value. Please have a look at the following code and suggest what's…
Vakas
  • 6,291
  • 3
  • 35
  • 47
-4
votes
1 answer

Class with Dependency injection vs Protocol with implementation

There are two examples below that produce the same result. Trying to understand which one is better in which situations (Pros and Cons). (1) Two classes with Dependency injection class ServiceClass { init() {} func printHelloWorld() { …
Polisas
  • 491
  • 1
  • 9
  • 20
-5
votes
1 answer

How to fix `Implementation of 'Protocol' cannot be automatically synthesized in an extension`?

I'm getting a compiler error while adding new protocol conformance to an extension. struct EquatableStruct { } extension EquatableStruct: Equatable { static func == (lhs: EquatableStruct, rhs: EquatableStruct) -> Bool { return true …
Frankenstein
  • 15,732
  • 4
  • 22
  • 47
-9
votes
1 answer

Why can't you rename the body property?

In swiftUI, in order to conform to the View protocol, the conforming struct has to implement the body property (specifically, its getter, since body property is read-only). Can I change the name of body property to something else, say…
NightFuryLxD
  • 847
  • 5
  • 15
1 2 3
75
76