Introduction
Clean Swift or Clean Architecture is an Architecture applied to iOS and Mac projects. The Clean Swift Architecture is not a framework. It is a set of Xcode templates to generate the Clean Architecture components for you. That means you have the freedom to modify the templates to suit your needs. So if we want to change something it doesn't affect with other files, because it is based on layers.
Swift is a powerful and intuitive programming language for macOS, iOS, watchOS and tvOS. Writing Swift code is interactive and fun, the syntax is concise yet expressive, and Swift includes modern features developers love. Swift code is safe by design, yet also produces software that runs lightning-fast. Swift already supports all Apple platforms as well as Linux, with community members actively working to port to even more platforms.
Tradition suggests that the first program in a new language should print the words “Hello, world!” on the screen. In Swift, this can be done in a single line:
print("Hello, world!")
Swift syntax is similar to C or objective-C. Code written at global scope is used as the entry point for the program, so you don’t need a main() function. You also don’t need to write semicolons at the end of every statement.
Use let to make a constant or var to make a variable. The value of the constant doesn't need to be known at compile time, but you must assign it a value exactly once. A constant or variable must have the same type as the value you want to assign to it.
var myVariable = 42
myVariable = 50
let myConstant = 42
Swift with SnapKit
SnapKit has a clear and concise API that makes writing constraints in the code a breeze. So
Installation
We will use cocoapods to install SnapKit. CocoaPods is a dependency manager for Swift and Objective-C Cocoa projects. It has over 51 thousand libraries and is used in over 3 million apps. CocoaPods can help you scale your projects elegantly.
# Xcode 8 + 9
$ sudo gem install cocoapods
Then in Xcode project directory, open terminal which is in your project directory location and type:
pod init
Then in the project directory you will see one podfile which we need to add:
pod 'SnapKit'
add this in the podfile and next you just go to your terminal and type:
pod install
So to use SnapKit you just import it in at the top of the file we want to use it
import SnapKit
Laying out a subview in its superview
let subview = UIView()
view.addSubview(subview)
subview.snp.makeConstraints { (make) in
make.top.equalTo(view)
make.bottom.equalTo(view)
make.left.equalTo(view)
make.right.equalTo(view)
}
This will set constraints for the top, bottom, left, and right edges of the subview to the corresponding edges of its superview with a constant of 0.
We can also constrain the size of my subview. Below, I’m going to set the height and width of the subview and set it to the center of its superview:
subview.snp.makeConstraints { (make) in
make.width.equalTo(200)
make.height.equalTo(200)
make.centerX.equalTo(view)
make.centerY.equalTo(view)
}
That example is pretty simple, but I repeated myself a lot. When setting constraints that have the same values, SnapKit allows me to chain constraints together like so:
subview.snp.makeConstraints { (make) in
make.width.height.equalTo(200)
make.centerX.centerY.equalTo(view)
}
This will produce same result as the above code.
Sample Counter App
This sample project will count the number if we click on plus sign and it will reduce when we click on minus sign like the above image shown. In this project we use SnapKit, ReactorKit and RxSwift libraries. So first install dependency:
add this to podfile,
pod 'SnapKit'
pod 'ReactorKit'
pod 'RxSwift'
and then run command:
pod install
In this project we follow the Clean Architecture,it is clean and more easier when working as a team or making changes to any files, then it doesn't affect to other files.
Here is the sample code in github: