Function That Takes a Protocol and a Conforming Class (!) Instance as Parameters

Swift - Function where parameter must conform to protocol and check is kindOf

the answer is to use order is ExistingOrder, crediting @rmaddy then check the return if let order = order as? ExistingOrder, crediting @dan

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)
}

Why extend class instead of conforming to a protocol?

Technically (i.e., in effect), there is no difference. The two methods will behave the same. However, this allows the programmer to put all the code required for the protocol in one place.

tl;dr: it's a stylistic choice.

Protocol Oriented Programming Optional Functions

In situation 1, Responsibility == Any, so both protocols are fulfilled.

In situation 2, you've implemented one requirement (Son), but not the other (Father). Father requires the method that accepts Any, and Guy doesn't implement that.

"Inheriting" a protocol just means "in order to conform to this protocol, a type must also conform to this other protocol." It's nothing like subclassing.

This design of protocols is probably very unhelpful, so I wouldn't continue down this road. If you find yourself passing around Any in a protocol, you're probably on a bad path already. This is not Protocol Oriented Programming. It's just "defining some protocols." But this is why it compiles or doesn't.

Can I get the name of a type conforming to a protocol from that protocol?

Naturally it's printing T, that's what you asked for with String(describing: T.self). T is always the protocol itself.

Inside the protocol extension Self (capital 'S') is how you refer to the conforming type.

So the extension should be:

extension T {
var typeName: String {
return String(describing: Self.self)
}
}

Aside, the built-in type(of:) function already gives you the dynamic type of any object, so it's not clear that you really need to duplicate this functionality on your own.

Conforming to a static function that returns Self

Self in a protocol is a requirement that conformance of the protocol use their own type. So you need to change Self to NameData in the return type of the method when you conform this in your class extension.

extension NameData: NSItemProviderReading {
static func object(withItemProviderData data: Data, typeIdentifier: String) throws -> NameData {
return NameData(name: "Test")
}
}

And one more thing, you need to make your NameData class final.

Problem writing a generic function that accepts any value that is RawRepresentable by a CustomStringConvertible

You should say that it conforms the protocol

where R.RawValue: CustomStringConvertible 

Now it works also for other types

enum MyEnum2: Int {
case one = 1
}

myFunction(val: MyEnum2.one)


Related Topics



Leave a reply



Submit