Swift protocol extension method is called instead of method implemented in subclass

protocol MyProtocol {

func methodA()

func methodB()

}

extension MyProtocol {

func methodA() {

print("Default methodA")

}

func methodB() {

methodA()

}

}

// Test 1

class BaseClass: MyProtocol {

}

class SubClass: BaseClass {

func methodA() {

print("SubClass methodA")

}

}

let object1 = SubClass()

object1.methodB()

//

// Test 2

class JustClass: MyProtocol {

func methodA() {

print("JustClass methodA")

}

}

let object2 = JustClass()

object2.methodB()

//

// Output

// Default methodA

// JustClass methodA

This is just how protocols currently dispatch methods.

A protocol witness table (see this WWDC talk for more info) is used in order to dynamically dispatch to implementations of protocol requirements upon being called on a protocol-typed instance. All it is, is really just a listing of the function implementations to call for each requirement of the protocol for a given conforming type.

Each type that states its conformance to a protocol gets its own protocol witness table. You‘ll note that I said "states its conformance", and not just "conforms to". BaseClass gets its own protocol witness table for conformance to MyProtocol. However SubClass does not get its own table for conformance to MyProtocol – instead, it simply relies on BaseClass‘s. If you moved the

: MyProtocol down to the definition of SubClass, it would get to have its own PWT.

So all we have to think about here is what the PWT for BaseClass looks like. Well, it doesn‘t provide an implementation for either of the protocol requirements methodA() or methodB() – so it relies on the implementations in the protocol extension. What this means is that the PWT for BaseClass conforming to MyProtocol just contains mappings to the extension methods.

So, when the extension methodB() method is called, and makes the call out to methodA(), it dynamically dispatches that call through the PWT (as it‘s being called on a protocol-typed instance; namely self). So when this happens with a SubClass instance, we‘re going through BaseClass‘s PWT. So we end up calling the extension implementation of methodA(), regardless of the fact that SubClass provides an implementation of it.

Now let‘s consider the PWT of JustClass. It provides an implementation of methodA(), therefore its PWT for conformance to MyProtocol has that implementation as the mapping for methodA(), as well as the extension implementation for methodB(). So when methodA() is dynamically dispatched via its PWT, we end up in its implementation.

As I say in this Q&A, this behaviour of subclasses not getting their own PWTs for protocols that their superclass(es) conform to is indeed somewhat surprising, and has been filed as a bug. The reasoning behind it, as Swift team member Jordan Rose says in the comments of the bug report, is

Therefore if this was the behaviour, already-compiled subclasses would lack any PWTs from superclass conformances that were added after the fact in another module, which would be problematic.

As others have already said, one solution in this case is to have BaseClass provide its own implementation of methodA(). This method will now be in BaseClass‘s PWT, rather than the extension method.

Although of course, because we‘re dealing with classes here, it won‘t just be BaseClass‘s implementation of the method that‘s listed – instead it will be a thunk that then dynamically dispatches through the class‘ vtable (the mechanism by which classes achieve polymorphism). Therefore for a SubClass instance, we‘ll wind up calling its override of methodA().

https://stackoverflow.com/questions/44703205/swift-protocol-extension-method-is-called-instead-of-method-implemented-in-subcl

原文地址:https://www.cnblogs.com/feng9exe/p/9680912.html

时间: 2024-08-03 09:13:01

Swift protocol extension method is called instead of method implemented in subclass的相关文章

Swift 使用Extension 场景 浅析

别人一看到我的 Swift 代码,立刻就会问我为什么如此频繁的使用 extension.这是前几天在我写的另一篇文章中收到的评论: 我大量使用 extension 的主要目的是为了提高代码可读性.以下是我喜欢使用 extension 的场景,尽管 extension 并非是为这些场景设计的. 私有的辅助函数 在 Objective-C 中,我们有 .h 文件和 .m 文件.同时管理这两个文件(以及在工程中有双倍的文件)是一件很麻烦的事情,好在我们只要快速浏览 .h 文件就可以知道这个类对外暴露的

(细节控)swift3.0与融云IMKIT开发问题(一部分) override func onSelectedTableRow Method does not override any method from its superclass

原官网文档方案如下,在swift3.0的情况下出现 override func onSelectedTableRow  Method does not override any method from its superclass 这是因为swift3.0 有很多变更,需要更换下onSelectedTableRow参数. //重写RCConversationListViewController的onSelectedTableRow事件 override func onSelectedTableR

接口测试基础知识详解http请求由三部分组成,分别是:请求行、消息报头、请求正文 1、请求行以一个方法符号开头,以空格分开,后面跟着请求的URI和协议的版本,格式如下:Method Request-URI HTTP-Version CRLF 其中 Method表示请求方法;Request-URI是一个统一资源标识符;HTTP-Version表示请求的HTTP协议版本;CRLF表示回车和换行(除了

HTTP URL (URL是一种特殊类型的URI,包含了用于查找某个资源的足够的信息)的格式如下:http://host[":"port][abs_path]http表示要通过HTTP协议来定位网络资源:host表示合法的Internet主机域名或者IP地址:port指定一个端口号,为空则使用缺省端口80:abs_path指定请求资源的URI:如果URL中没有给出abs_path,那么当它作为请求URI时,必须以"/"的形式给出,通常这个工作浏览器自动帮我们完成.e

LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method, and this method cannot be translated into a store expression

if (!string.IsNullOrEmpty(FarmWorkId)) { data = data.Where(p => p.TypeId == Convert.ToInt32(FarmWorkId)); } 解决方法: if (!string.IsNullOrEmpty(FarmWorkId)) { int i = Convert.ToInt32(FarmWorkId); data = data.Where(p => p.TypeId == i); } LINQ to Entities

Swift:Extension和Protocol

一.Swift中的扩展功能(Extension)可以动态给类增加功能,类似于Javascript中的prototype; 而在objective-c中,是通过runtime来实现动态调用或者添加功能的. 定义方式如下: extension SomeType { } 1. 单位转换 extension Double { var km: Double {return self * 1000.0} } 调用的代码如下: let walk: Double = 25.4; print("25.4千米 =

Swift -- protocol、extension

protocol ExampleProtocol{ var simpleDescription:String{get} mutating func adjust() } class SimpleClass:ExampleProtocol{ var simpleDescription:String = "A very simple class" var anotherProperty:Int = 69105 func adjust(){ simpleDescription += &quo

swift protocol 见证容器 虚函数表 与 动态派发

一.测试代码: //protocol DiceGameDelegate: AnyObject { //} // //@objc protocol OcProtocol{ //    @objc func OcFunc() //} protocol SeedProtocol { func NormalFunc() func ExtenImpFunc() //@objc func OcFunc() } extension SeedProtocol{ func ExtenImpFunc(){} fun

Swift 扩展(Extension)总结

扩展是给已经存在的类(class),结构体(structure),枚举类型(enumeration)和协议(protocol)增加新的功能.类似Objective-C中的Category,不同的是,Extension没有名字.扩展可以做以下事情: 增加计算实例属性和计算类型属性 定义实例方法和类型方法 提供新的初始化器 定义下标 定义和使用新的内置类型 让一个存在的类型服从一个协议 注:扩展可以增加新的功能,但是不能覆盖已有的功能 和oc相比是阉割版本. 原文地址:https://www.cnb

swift protocol(协议) associatedtype关联类型

定义一个协议时,有的时候声明一个或多个关联类型作为协议定义的一部分将会非常有用.关联类型为协议中的某个类型提供了一个占位名(或者说别名),其代表的实际类型在协议被采纳时才会被指定.你可以通过 associatedtype 关键字来指定关联类型.比如使用协议声明更新cell的方法: //模型 struct Model { let age: Int } //协议,使用关联类型 protocol TableViewCell { associatedtype T func updateCell(_ da