Swift中Double Protocol Conformance的陷阱

前言

一般情况下,我们修复问题都会将我们能想到的错误case处理,但有时还是会有意想不到的副作用产生。这篇文章介绍的就是Double Protocol Conformance带来的问题

什么是 Double Protocol Conformance

简单说就是一个类型被声明多次遵循相同协议

swift 复制代码
// Item 已经遵循了Codable协议
public struct Item: Codable { } 
// Error: Redundant conformance of 'Item' to protocol 'Decodable'
// Error: Redundant conformance of 'Item' to protocol 'Encodable'
// Item 的扩展遵循了Codable协议
extension Item: Codable { }

如果我们在同一个Target里面书写上面的代码会报错

有趣的部分

  1. 我们在同一个Target里面,对一个类型和这个类型的扩展同时声明遵循相同协议,Xcode编译器会检测出来并报错
  2. 但当我们在不同的Target里分别对一个类型和这个类型的扩展声明遵循相同协议时,Xcode就检测不出来了

在TargetA中声明Item遵循Codable,并使用编译器的默认实现

swift 复制代码
// TargetA
public struct Item: Codable { 
    public let id: UUID
    public let name: String 
    public init(id: UUID, name: String) { 
        self.id = id 
        self.name = name 
    }
}

在TargetB中引入TargetA,并在Item的扩展中声明遵循Codable协议,自定义实现编解码

swift 复制代码
// TargetB
import Foundation
import TargetA 
extension Item: Codable { 
    // MARK: - Coding Keys 
    enum CodingKeys: String, CodingKey {
        case id = "product_id"
        case name = "product_name"
    } 
    // MARK: - Encodable 
    public func encode(to encoder: Encoder) throws { 
        var container = encoder.container(keyedBy: CodingKeys.self) 
        try container.encode(id, forKey: .id) 
        try container.encode(name, forKey: .name) 
    } 
    // MARK: - Decodable 
    public init(from decoder: Decoder) throws { 
        let values = try decoder.container(keyedBy: CodingKeys.self) 
        let id = try values.decode(UUID.self, forKey: .id) 
        let name = try values.decode(String.self, forKey: .name) 
        self = .init(id: id, name: name) 
    }
}

此时Xcode不会报错,而只会弹出警告。但就隐藏着问题了。只要代码能Run,我们就会认为没啥问题

暴露问题

假设我们有3个Target

  1. TargetA里面声明了Item,但是没有遵循Codable协议
  2. TargetB依赖TargetA,并在Item的扩展中遵循了Codable协议
  3. TargetC依赖TargetA,并未依赖TargetB,也在Item的扩展中遵循了Codable协议

看起来我们的代码好像没啥问题,我们也很难觉察到有Double Protocol Conformance的问题

如果我们在另外的Target里引入了TargetA、TargetB、TargetC,引入的顺序会影响最终的实现

swift 复制代码
// 先引入B再引入C
import TargetA
import TargetB
import TargetC 
let item = Item(id: .init(), name: "My Item") 
let encoder = JSONEncoder()
let encoded = try encoder.encode(item)
let jsonString = String(data: encoded, encoding: .utf8)

/** 输出的结果如下
{ 
    "product_name_B": "My Item", 
    "product_id_B": "42A18A37-51FA-422C-9BB9-46D382CDF1CA"
}
*/
swift 复制代码
// 先引入C再引入B
import TargetA
import TargetC
import TargetB
let item = Item(id: .init(), name: "My Item") 
let encoder = JSONEncoder()
let encoded = try encoder.encode(item)
let jsonString = String(data: encoded, encoding: .utf8)

/** 输出的结果如下
{ 
    "product_name_C": "My Item", 
    "product_id_C": "42A18A37-51FA-422C-9BB9-46D382CDF1CA"
}
*/
  1. 从上面的例子中,我们可以知道当一个类在不同的Target中同时遵循协议的时候,采用的是实现是第一个import中的实现
  2. 我们一般在工作中都是模块化的,会比例子里面的情况更复杂。所以对于协议遵循指定一套规则是很重要的

资料

alexanderweiss.dev/blog/2023-0...

相关推荐
菜的不敢吱声2 小时前
swift学习第4天
服务器·学习·swift
菜的不敢吱声1 天前
swift学习第2,3天
python·学习·swift
大熊猫侯佩2 天前
拒绝“假死”:为何上滑关闭是测试大忌?揭秘 iOS 真实 OOM 触发指南
app·swift·apple
大熊猫侯佩2 天前
Swift 6.2 列传(第十六篇):阿朱的“易容术”与阿紫的“毒药测试”
swift·编程语言·apple
麦兜*2 天前
【Swift】苹果App开发全流程解析:从Xcode配置到App Store上架避坑指南
xcode·swift
JQShan3 天前
Core Data 简化开发:NSPersistentContainer 从原理到实战
swift
东坡肘子3 天前
2026:当 AI 隐入工作流,你准备好了吗? -- 肘子的 Swift 周报 #117
人工智能·swiftui·swift
菜的不敢吱声4 天前
swift学习第一天
开发语言·学习·swift
大熊猫侯佩5 天前
2026 码农漫游:AI 辅助 Swift 代码修复指南
swift·编程语言·apple
大熊猫侯佩5 天前
Swift 6.2 列传(第十五篇):王语嫣的《万剑归宗》与 InlineArray
swift·编程语言·apple