在开发应用程序时,我使用扩展来隐藏对代码中特定库的依赖 . 我希望方法名称与库中的名称完全相同 .
这种方法有助于大大减少对外部库的依赖(用不同的方式交换它,我只需要重写桥中的函数) . 但是,在扩展的情况下,它有点棘手,因为我使用此代码获得无限循环:
// Library implementation, File1.swift
import Foundation
extension Date {
func test(otherDate: Date) -> String {
return String()
}
}
// App reference to the library, different module, File2.swift
import Foundation
import Library
extension Date {
func test(otherDate: Date) -> String {
// How to reference "Library" here?
return test(otherDate: otherDate)
}
}
添加前缀时,方法可以正常工作:
// Works correctly:
import Foundation
import Library
extension Date {
func prefix_test(otherDate: Date) -> String {
return test(otherDate: otherDate)
}
}
Is there any way in Swift to call an extension method from a particular module, similar to selecting a class :
// Selecting a class:
let a = Module1.CustomClass()
let b = Module2.CustomClass()
let date = Date()
// Can I select an extension method?
date.Module1.func1()
date.Module2.func1()
How is this question different from the "duplicate": 如果是类功能,可以这样做:
Module1.Class1.classFunction()
Module2.Class1.classFunction()
In the aforementioned case, such call is impossible, hence the question.