Class Conforming to Protocol as Function Parameter in Swift

Class conforming to protocol as function parameter in Swift

You can define foo as a generic function and use type constraints to require both a class and a protocol.

Swift 4

func foo<T: UIViewController & UITableViewDataSource>(vc: T) {
.....
}

Swift 3 (works for Swift 4 also)

func foo<T: UIViewController>(vc:T) where T:UITableViewDataSource { 
....
}

Swift 2

func foo<T: UIViewController where T: UITableViewDataSource>(vc: T) {
// access UIViewController property
let view = vc.view
// call UITableViewDataSource method
let sections = vc.numberOfSectionsInTableView?(tableView)
}

Class conforming to protocol as function parameter in Swift Error

The error is a bit misleading but you are missing the argument label. You need to do this:

foo(view: MyView())

or if you want to remove the need for it you could change the method definition to this:

func foo<T: UILabel>(_ view:T) where T: HeaderDisplayable{
view.setTitle("HEY")
}

Either version will work fine.

Type of a class which conforms to a protocol as parameter in a method swift

In your code target contains a "runtime type object". And you cannot use such thing as a generic type argument.
You know you cannot do this:

func doStuff<C: MyProtocol>(target: C.Type) {
var c: target? //<- This is illegal. You cannot use `target` where a "type" is needed.
}

Why don't you write it as:

func doStuff<C: MyProtocol>(target: C.Type) {
SomeClass<C>.doSomething()
}

You can use it as:

myObj.doStuff(ConformingClass.self)

Swift cannot pass class conforming to protocol as function parameter to a function residing in Objective-C file

You probably don't mean to say this:

-(void)setWithFoo:(id<FooProtocol>*)_foo;

It is very unusual to see an id* in Objective-C. In fact, it's so unusual that in all my years of programming Cocoa, I have never seen one.

You probably mean this:

-(void)setWithFoo:(id<FooProtocol>)_foo;

And then you will be able to say, on the Swift side:

objcObject.setWithFoo(myObject)

Class conform to protocol with function that receives a generic parameter

The code you've written can't be correct because VariableCell requires that any CellDataType be passable to setUp, but then PictureCell only permits one specific types to be passed. If you passed a VariableCell to a function, what would it have to pass to setUp?

But if you're really trying to express that PictureCell conforms to VariableCell by setting T == PictureData (rather than by VaraibleCell accepting all possible T), then that's exactly what associatedtypes are for:

protocol VariableCell {
associatedType CellData: CellDataType
func setUp(cellData: CellData)
}

class PictureCell: VariableCell {
func setUp(cellData: PictureData) {
}
}

class TextCell: VariableCell {
func setUp(cellData: TextData) {
}
}

Once you do this, you can no longer use VariableCell as a normal type. For example, you cannot have a [VariableCell], because you would not be able to loop over them and call setUp (what would you pass if the types can be different)? But assuming that you intend to build generic code that relies on VariableCell, then this is exactly the tool you want.

Swift class conforming to protocol, how to pass it as a parameter?

You could use generics and generic contstraints.

func getBLEInstance<T: BLEScannerProtocol>(media: T) -> BLEScanner {
media.someTypeMethod()
}

This says the function takes a generic type, but one that implements BLEScannerProtocol.

EDIT for request in comment.

You can achieve what you want, you'll just need to move the generic type up to the class level and create a property to hold the view controller. Here is a simplified example.

protocol BLEScannerProtocol {
func someTypeMethod() -> Double
}

class BLEScanner<T: BLEScannerProtocol> {
var media: T!

func getBLEInstance(m: T) -> BLEScanner {
media = m
}

func someOtherMethod() {
media.someTypeMethod()
}
}

Notice that media either needs to be an optional or implicitly unwrapped optional since it can't have a value set during init.

Pass an array of protocol conforming objects to function

As others have said you want to have func execute(requests: [Request]) {} in this case.

The reasoning is that func execute<T: Request>(requests: [T]) {} as a generic function is saying you want a concrete known type T that conforms to the Request protocol.

The change to [Request] from [T] lets you have an array of any type that conforms to Request instead of an array of one type that conforms.



Related Topics



Leave a reply



Submit