public protocol SomeInterface {
func doSomething()
}
和实施包
public struct SomeImplementation: SomeInterface {
func doSomething() {
//stuff happens here
}
}
实现程序包必须将接口程序包作为依赖项包含在Package.swift文件中
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(url: "https://example.com/some-interface.git", from: "1.2.3"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "SomeImplementation",
dependencies: ["SomeInterface"]),
.testTarget(
name: "SomeImplementation Tests",
dependencies: ["SomeImplementation"]),
]
1条答案
按热度按时间xzabzqsa1#
你可以创建两个单独的swift包。一个包只是协议(接口),另一个包是符合协议的结构或类(实现)。例如...
接口包
和实施包
实现程序包必须将接口程序包作为依赖项包含在Package.swift文件中
然后,您可以注入符合协议的实现,而只需要将协议包添加到模块中。
这样,只要接口包没有任何重大更改,您的实现包就可以随着版本更新而发展,而不必更新项目中依赖于接口的模块。