Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
549 views
in Technique[技术] by (71.8m points)

swift - Forced to cast, even if protocol requires given type

I have following code:

import UIKit

protocol Fooable: class where Self: UIViewController {
    func foo()
}

class SampleViewController: UIViewController, Fooable {

    func foo() {
        print("foo")
    }
}

let vc1: Fooable = SampleViewController()
let vc2: Fooable = SampleViewController()


// vc1.show(vc2, sender: nil) - error: Value of type 'Fooable' has no member 'show'

// (vc1 as! UIViewController).show(vc2, sender: nil) - error: Cannot convert value of type 'Fooable' to expected argument type 'UIViewController'

(vc1 as! UIViewController).show((vc2 as! UIViewController), sender: nil)

commented lines doesn't compile.

Why am I forced to cast protocol type object to UIViewController even if Fooable protocol requires, that types that conform to it inherit from UIViewController?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Adopting the protocol Fooable tells the compiler that this particular UIViewController responds to foo(), no less no more.

In reverse conclusion Fooable does not become UIViewController necessarily.

The constraint Self: UIViewController is just another information for the compiler to complain at compile time if the affected class is not UIViewController

In your case when annotating SampleViewController to Fooable the compiler knows only that SampleViewController responds to foo(). It does not know that the type is actually a subclass of UIViewController.

So do not annotate a concrete class to a protocol if you want to access properties of the concrete class.

However you could add the show method and other common properties / methods to the protocol

protocol Fooable: class where Self: UIViewController {
    func foo()
    func show(_ vc: Fooable, sender: Any?)
}

then you can use Fooable because the compiler knows that the type adopting the protocol responds to the method.


A suitable practice to annotate a type to a protocol is for example when you are going to create a heterogenous but restricted collection type

let array : [CustomStringConvertible] = ["Foo", 1, false]
array.forEach{ print("($0)")}

The code prints the three items using the description property which all items respond to. The compiler recognizes the three items as types which have a description property, not as String, Int and Bool.

Update:

In Swift 5 support of superclass constrained protocols is implemented.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...