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

Protocol extension in Swift 3

I want to have a default property of UIImageView, which would be isFlipped. I am able to do it by subclassing UIImageView and adding one property isFlipped. But I want to user protocol and extensions for this , but it is crashing after sometime.…
Sishu
  • 1,510
  • 1
  • 21
  • 48
-1
votes
1 answer

Value of type NSObject -> () -> **ViewController does not conform to specified type ***Delegate

I am receiving the error below: My protocol looks like this: protocol RecorderDelegate { func finishedRecordingWithUrl(URL: NSURL) } Can someone explain why self is NOT conforming to the protocol when it appears that it is?
etayluz
  • 15,920
  • 23
  • 106
  • 151
-1
votes
2 answers

How to determine the necessary functions of a delegate?

How do I know which functions need to be implemented by a class to conform to a protocol? E.g. FBSDKAppInviteDialogDelegate requires appInviteDialog: didCompleteWithResults and appInviteDialog:didFailWithError:. Xcode only gives an error without…
Manuel
  • 14,274
  • 6
  • 57
  • 130
-1
votes
1 answer

Creating a Scroll View Protocol in swift 2.2

I am currently developing an iOS application with login and sign up forms. To make sure that the keyboard does not cover any UITextFields I've implemented the following solution provided by Apple and discussed in this issue. To briefly sum it up,…
-2
votes
1 answer

In Swift, how do I add a where constraint is equatable inside a protocol?

I want to write a generic deck of cards class which I can use in different applications and it currently has a protocol but I am unable to add a where Element : Equatable to the protocol as it complains that: Instance method requirement 'find'…
zardon
  • 1,601
  • 4
  • 23
  • 46
-2
votes
1 answer

Can't return concrete type with generic argument in extension method

How can I create such createFetcher method returning object under the generic protocol? Can't understand what's unclear here to compiler. Such approach worked fine without associatedtype in protocol. Below code won't compile with error: Cannot…
bodich
  • 1,708
  • 12
  • 31
-2
votes
1 answer

Swift declare generic protocol by "="

@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *) public protocol ObservableObject : AnyObject { /// The type of publisher that emits before the object has changed. associatedtype ObjectWillChangePublisher : Publisher =…
kkklc
  • 149
  • 7
-2
votes
1 answer

Struggling with Swift Protocol and `some` Keyword

I have a Processor protocol: protocol OutputProvider: AnyObject { associatedtype OutputType var cachedOutput: OutputType? { get set } } protocol Processor: OutputProvider { associatedtype InputType associatedtype…
Zaggo
  • 786
  • 1
  • 6
  • 14
-2
votes
2 answers

Swift 5.6: using opaque type with protocols and associate types

I'm trying to use the 'some' keyword with protocols and associate types as shown hereafter (Swift 5.6). protocol Foo { associatedtype yep func yo(_ a:yep) } struct A: Foo { func yo(_ a:String) { print(a) } } var a: some Foo…
XLE_22
  • 5,124
  • 3
  • 21
  • 72
-2
votes
2 answers

Typecasting to Generic class in Swift

Im trying to typecast an object to a class that uses Generics. Here is some code for better understanding I've a protocol named wheel protocol Wheel I've a class named Wings class Wings { var count = 2 } Now, I have a generic class named…
Nari
  • 333
  • 5
  • 13
-2
votes
1 answer

Some Problems about Opaque Return Type and Protocol with associatedtype In Swift

How to deal with this problem? Definitions: protocol NameProtocol { var rawValue: String { get } } struct CatName: NameProtocol { enum CatNamesEnum: String { case Tiger, Max, Sam } var literal: CatNamesEnum var rawValue:…
Silo QIAN
  • 60
  • 8
-2
votes
1 answer

protocol extension use where clause doesn't work in Swift

We know in Swift, we can use where clause in protocol extension: protocol Ordered { func precedes(other: Self) -> Bool } func binarySearch(sortedKeys: [T], forKey k: T) -> Int { 11 } // I want make all Comparable…
hopy
  • 559
  • 3
  • 18
-2
votes
1 answer

Swift Result<> cannot convert to downcast result

I'm using the swift Result<> generic in a function completion block, but passing it to another completion block with a protocol type doesn't work as expected. public protocol Journey { } class CommsJourney: Journey { } typealias Completion =…
Darren
  • 10,182
  • 20
  • 95
  • 162
-2
votes
1 answer

Swift - Inheriting protocol does not inherit generic where constraint

I am trying to migrate to using protocols instead of subclassing, but I still would like to be able to delegate up to the "super" implementation of a certain method before performing more specific logic. Below is the code I've written to demonstrate…
-2
votes
1 answer

What is the purpose of having property in a protocol?

I have created a protocol as below protocol FullyNamed { var fullName: String { get } } Below my class which confirm this protocol class Starship: FullyNamed { var prefix: String? var name: String init(name: String, prefix:…
iOSGuy
  • 171
  • 1
  • 14
1 2 3
75
76