Helper | ||
Images | ||
.gitignore | ||
.swiftlint.yml | ||
LICENSE | ||
README.md | ||
swift.jpeg |
Useful Swift Things
Content
- Coding convention
- Design Pattern
- Code Structuring
- UIDesign
- Helper
- API
- JSON
- Third Party Library
- GCD
- Useful Stuff
Coding convention
set of guidelines for a specific programming language that recommend programming style
Swift Style Guide
Swift Lint
The way of force you to adapt coding convention
otherwise project build will FAILED
- Swift Lint apply for all project👍
if which swiftlint >/dev/null; then
swiftlint
else
echo "error: SwiftLint not installed, download from https://github.com/realm/SwiftLint"
exit 1
fi
put .yml file into root folder and apply following code in Build Phases
You can modify(delete) SwiftLint Option with opening .yml file
Shift + Command + . will show the hidden file
Design Pattern
Design pattern is
Check this website for design pattern in Swift
Delegate Pattern
weak var delegate: SomeProtocol?
Singleton Pattern
class SingletonPattern {
static let manager = SingletonPattern()
private init() {}
}
Observer Pattern
The observer pattern lets one object observe changes on another object. Apple added language-level support for this pattern in Swift 5.1 with the addition of Publisher in the Combine framework.
Code Structuring
MVC
MVVM
MVC vs MVVM
M -> Model – Which holds the application data
V –> View – It displays the data that is stored in model. These are visual elements through which a user interacts. These are subclasses of UIView
VM –> View Model – Transform model information/data and it interacts with controller or view to display those informations.
C –> Controller class – It will be there but the responsibility of view business logic has been removed and give to view model
VIPER
UIDesign
HIG(Human Interface Guidelines)
iOS icon
UIdesign inspiration
Helper
All files are resuable files and protocol oriented. Just Copy and Paste inside your project and use it!! 👍
Email, Message, Call
You can check the file in the follow link
Usage
import MesaageUI first
import MessageUI
Then use it
Don't forget to extend the mail, message delegate to your ViewController!
lazy var conversation = ConversationManager(presentingController: self, mailDelegate: self, messageDelegate: self, viewController: self)
@IBAction private func sendEmail(_ sender: UIButton) {
conversation.sendEmail(feedback: MailFeedback(recipients: ["abcd@google.com"], subject: "FeedBack", body: "Write feedback here"))
}
@IBAction private func sendMessage(_ sender: UIButton) {
conversation.sendMessage(feedback: MessageFeedBack(recipients: ["1111111111"], body: "Type here"))
}
@IBAction private func startCall(_ sender: UIButton) {
conversation.makeCall(number: "1111111111")
}
Good To GO 👏👏👏
See Example here
Network Layer
Usage
First, set the base URL in EndPointType file
Don't forget to put your API key in it!
var baseURL: URL {
guard let url = URL(string: "https://api.openweathermap.org/data/2.5/") else {
fatalError("baseURL could not be configured.")
}
return url
}
then make a instance of router.swift file in your code
private let router = Router<YourAPI>()
for YourAPI part, simply create a new enum with cases about specific api URL
It will make your router more dynamic! Don't forget extension to EndPointType!
enum YourAPI {
case first(country: String)
case second(time: Int)
case third(name: String)
}
extension YourAPI: EndPointType {
var path: String {
switch self {
case .first(let country):
return "\(country).json"
case .second(let time):
return "\(time).json"
case .third(let name):
return "\(name).json"
}
}
}
then, use it like this
router.request(.first(country: London)) { [weak self] (results: Result<CountryWeather, AppError>) in
guard let self = self else { return }
switch results {
case .success(let data):
// insert your modifications!
case .failure(let error):
// insert your modifications!
print(error)
}
}
CountryWeather should be a model with Decodable
This reusable network layer files for referenced from here
Image Picker
Usage
Copy and Paste in your project and then declare Image Picker object inside your project
lazy var imagePicker = ImagePicker(presentationController: self, delegate: self)
Then, extend ImagePickerDelegate to your viewController
extension ViewController: ImagePickerDelegate {
func didSelect(image: UIImage?) {
self.yourImageView.image = image
self.dismiss(animated: true, completion: nil)
}
}
Good To GO 👏👏👏
See Example here
File Manager
Usage
Copy and Paste in your project
let readData = FileManageHelper.manager.readFile(filename: fileNameTextField.text ?? "", type: extensionTextField.text ?? "")
resultTextField.text = readData
File Manager are wrote with singleton pattern, therefore no need to declare in side your code!
Good To GO 👏👏👏
Video Downloader
Usage
Make an object of VideoManager inside your code
let videoManager = VideoManager()
use downloadVideoLinkAndCreateAsset function to start download with entering URL
self.videoManager.downloadVideoLinkAndCreateAsset(text)
Good To GO 👏👏👏
Location Manager
API
API(Application Programming Interface) is an interface that defines interactions between multiple software applications or mixed hardware-software intermediaries. It defines the kinds of calls or requests that can be made, how to make them, the data formats that should be used, the conventions to follow, etc.
JSON
JSON is a language-independent data format
Which is relative with KEY - VALUE pair
{
"main": [
{
"title": "example1",
"body": "body1"
},
{
"title": "example2",
"body: "body2"
}
]
}
JSON parser extension for Chrome
This extension makes JSON more structable JSON parser pro FREE 👍
JSON Decoding
To use JSON Decoding in swift, you have to define the model to be Codable or Decodable
public typealias Codable = Decodable & Encodable
Decodable can only decode the json data. Can't encoded json file!!
struct User: Codable {
var first_name: String
var last_name: String
var country: String
}
JSONSerialization
Various API Site
GCD
GCD(Grand Central Dispatch) is a low-level API for managing concurrent operations. It can help you improve your app’s responsiveness by deferring computationally expensive tasks to the background.
DispatchQueue
An object that manages the execution of tasks serially or concurrently on your app's main thread or on a background thread.
main
We can say main is a serial queue
global()
We can say global is a concurrent queue
DispatchGroup
DispatchWorkItem
Thread Sanitizer
Thread Sanitizer is a tool to identifies the potential thread-related corruption issues. And it is a good way to find the Readers and Writers problem in your application.
How to Use Address Sanitizer
Go to this Option and Click EDIT SCHEME... 👈
And then go to RUN and check THREAD SANITIZER 👈
Third Party Library
This github contains all the popular libraries in Swift👍
Useful Stuff
Show Preview in UIKit(Build UI with Code Base) 👍 👍 👍 👍 👍
Copy this code and Paste into your controller
#if canImport(SwiftUI) && DEBUG
import SwiftUI
struct SwiftLeeViewRepresentable: UIViewRepresentable {
func makeUIView(context: Context) -> UIView {
return UIStoryboard(name: "Main", bundle: Bundle.main).instantiateInitialViewController()!.view
}
func updateUIView(_ view: UIView, context: Context) {
}
}
@available(iOS 13.0, *)
struct SwiftLeeViewController_Preview: PreviewProvider {
static var previews: some View {
SwiftLeeViewRepresentable()
}
}
#endif
Enable canvas option like this
You are GOOD TO GO 👏👏👏
Write README.md
This will help you to write a README.md file more dynamically 👍
Author
This README.md file is written by Jungpyo Hong (Dennis)