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...

相关推荐
小鹿撞出了脑震荡3 天前
Effective Objective-C 2.0 读书笔记—— 方法调配(method swizzling)
ios·objective-c·swift
小鹿撞出了脑震荡3 天前
Effective Objective-C 2.0 读书笔记—— 消息转发
ios·objective-c·swift
一丝晨光4 天前
Cocoa和Cocoa Touch是什么语言写成的?什么是Cocoa?编程语言中什么是框架?为什么苹果公司Cocoa类库有不少NS前缀?Swift编程语言?
macos·ios·objective-c·cocoa·swift·uikit·cocoa touch
Swift社区7 天前
LeetCode - #195 Swift 实现打印文件中的第十行
vue.js·leetcode·swift
taopi20248 天前
ios swift画中画技术尝试
ios·xcode·swift
Swift社区8 天前
LeetCode - #194 Swift 实现文件内容转置
vue.js·leetcode·swift
taopi20249 天前
iOS swift 后台运行应用尝试失败
ios·xcode·swift
假装自己很用心13 天前
iOS 内购接入StoreKit2 及低与iOS 15 版本StoreKit 1 兼容方案实现
ios·swift·storekit·storekit2
大熊猫侯佩15 天前
Swift 趣味开发:查找拼音首字母全部相同的 4 字成语(下)
开发语言·正则表达式·字符串·swift·string·成语·文本解析
Johnny Tong16 天前
ReactiveSwift 简单使用
swift