AmazingCommand
is a spin on classic Command
design pattern with a Swift version of Target-Action
design pattern on top. Together they will enable you to create commands with ease, without unnecessary repetition and no chances of accidentally creating a retencion cycle.
Let's say that you are developing financial application and you have Command
pattern already applied. And it works fine for global commands like Transfer
and Deposit
. But now you want to create a command Export
that is specific to one of the application's screens. What do you do? You create a class that implements your command protocol. And here is where the problem arise. You just created a separate class for a command that is tightly coupled with one of many screens in the application. That's where AmazingCommand
comes to rescue.
import AmazingCommand
class ExportViewModel {
var exportCommand: AmazingCommand<ExportViewModel, IndexPath>!
init() {
exportCommand = AmazingCommand(target: self,
executeFunction: ExportViewModel.export,
canExecuteFunction: ExportViewModel.canExport)
}
func export(_ parameter: IndexPath) {
// ...
}
func canExport(_ parameter: IndexPath) -> Bool {
let canExport = true
// ...
return canExport
}
}
class ExportViewController: UIViewController, UITableViewDelegate {
let viewModel = ExportViewModel()
// ...
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
viewModel.exportCommand.execute(indexPath)
}
// ...
}
- macOS 10.14
- iOS 11.0
- tvOS 11.0
- watchOS 4.0
- Xcode 11
- Swift 5.0
To integrate AmazingCommand
into your Xcode project using Swift Package Manager, specify it as dependency in your Package.swift
:
dependencies: [
.package(url: "https://github.com/khamitimur/AmazingCommand.git", .upToNextMajor(from: "1.0.0"))
]
This project is licensed under the MIT License.
- Any similarities with the
DelegateCommand
are deliberate. - Implemenation of
Target-Action
design pattern in Swift was inspired by the article by Ole Begemann.