2021-04-10 16:21:15 +07:00
# Useful Swift Things
2021-04-10 15:42:23 +07:00
2021-04-10 16:27:15 +07:00
< div align = "center" >
2021-04-13 13:32:42 +07:00
< img src = "https://github.com/jphong1111/Useful_Swift/blob/main/Images/swift.jpeg" >
2021-04-10 16:27:15 +07:00
< / div >
2021-04-27 12:19:44 +07:00
< p >
< / p >
2021-04-27 12:11:04 +07:00
< p align = "center" >
< img src = "https://img.shields.io/badge/-OneDayOneCommit-critical?style=plastic&logo=swift" / >
< / p >
2021-04-13 13:33:49 +07:00
2021-04-10 16:15:30 +07:00
## Content
2021-04-10 04:14:59 +07:00
- [Coding convention ](#Coding-convention )
2021-04-10 16:05:27 +07:00
- [Design Pattern ](#Design-Pattern )
2021-04-11 02:05:11 +07:00
- [Delegate ](#Delegate )
- [Singleton ](#Singleton )
2021-04-27 11:39:49 +07:00
- [Observer ](#Observer-Pattern )
2021-04-10 16:18:23 +07:00
- [Code Structuring ](#Code-Structuring )
2021-04-10 16:20:15 +07:00
- [MVC ](#MVC )
- [MVVM ](#MVVM )
2021-04-27 11:39:33 +07:00
- [VIPER ](#VIPER )
2021-04-10 04:19:52 +07:00
- [UIDesign ](#UIDesign )
2021-04-22 17:06:56 +07:00
- [Helper ](#Helper )
2021-05-03 13:57:35 +07:00
- [Email, Message, Call ](#Email,-Message,-Call )
- [Network Layer ](#Network-Layer )
2021-04-10 04:19:52 +07:00
- [API ](#API )
2021-04-10 15:55:09 +07:00
- [JSON ](#JSON )
2021-04-10 04:30:48 +07:00
- [Third Party Library ](#Third-Party-Library )
2021-04-29 11:42:01 +07:00
- [GCD ](#GCD )
2021-04-30 13:40:48 +07:00
- [DispatchQueue ](#DispatchQueue )
- [DispatchGroup ](#DispatchGroup )
- [DispatchWorkItem ](#DispatchWorkItem )
2021-04-30 17:16:16 +07:00
- [Thread Sanitizer ](#Thread-Sanitizer )
2021-04-23 14:19:47 +07:00
- [Useful Stuff ](#Useful-Stuff )
2021-04-23 23:34:04 +07:00
- [Show Preview in UIKit(Build UI with Code Base) ](#show-preview-in-uikitbuild-ui-with-code-base----- )
2021-04-25 04:50:50 +07:00
- [Write README.md ](#write-readmemd )
2021-04-10 03:16:44 +07:00
2021-04-10 03:25:46 +07:00
2021-04-10 04:13:06 +07:00
## Coding convention
2021-04-10 15:22:54 +07:00
set of guidelines for a specific programming language that recommend programming style
### Swift Style Guide
2021-04-10 04:12:26 +07:00
2021-04-10 14:51:46 +07:00
- [Swift Style Guide ](https://github.com/linkedin/swift-style-guide )
2021-04-10 04:34:45 +07:00
### Swift Lint
2021-04-10 15:20:40 +07:00
The way of force you to adapt coding convention
>otherwise project build will **FAILED**
2021-04-10 15:14:13 +07:00
- [Swift Lint ](https://github.com/realm/SwiftLint ) apply for all project:+1:
2021-04-15 19:36:44 +07:00
```swift
2021-04-15 19:36:01 +07:00
if which swiftlint >/dev/null; then
swiftlint
else
echo "error: SwiftLint not installed, download from https://github.com/realm/SwiftLint"
exit 1
fi
```
2021-04-10 04:34:45 +07:00
put .yml file into root folder and apply following code in Build Phases
2021-04-10 03:47:49 +07:00
2021-04-22 17:19:49 +07:00
**You can modify(delete) SwiftLint Option with opening .yml file**
2021-04-22 17:20:10 +07:00
2021-04-22 17:21:49 +07:00
> Shift + Command + . will show the hidden file
2021-04-22 17:19:49 +07:00
< img src = "https://github.com/jphong1111/Useful_Swift/blob/main/Images/swiftLintChange.png" >
2021-04-10 16:11:11 +07:00
## Design Pattern
2021-04-14 04:00:57 +07:00
Design pattern is
2021-04-14 04:01:22 +07:00
2021-04-14 04:00:57 +07:00
Check [this ](https://refactoring.guru/design-patterns/swift ) website for design pattern in Swift
2021-04-10 16:11:11 +07:00
### Delegate Pattern
2021-04-10 16:30:44 +07:00
```swift
2021-04-10 16:11:11 +07:00
weak var delegate: SomeProtocol?
```
### Singleton Pattern
2021-04-10 16:30:44 +07:00
```swift
2021-04-10 16:11:11 +07:00
class SingletonPattern {
static let manager = SingletonPattern()
private init() {}
2021-04-10 16:11:45 +07:00
}
2021-04-10 16:11:11 +07:00
```
2021-04-14 04:02:14 +07:00
### Observer Pattern
2021-04-10 16:18:23 +07:00
## Code Structuring
2021-04-10 16:05:27 +07:00
2021-04-10 16:18:23 +07:00
### MVC
2021-04-10 16:05:27 +07:00
2021-04-15 10:00:46 +07:00
< img src = "https://github.com/jphong1111/Useful_Swift/blob/main/Images/MVCModel.png" >
2021-04-13 13:33:49 +07:00
2021-04-10 16:20:15 +07:00
### MVVM
2021-04-15 10:01:10 +07:00
##### MVC vs MVVM
2021-04-15 10:00:46 +07:00
< img src = "https://github.com/jphong1111/Useful_Swift/blob/main/Images/MVVMvsMVC.png" >
2021-04-19 15:39:24 +07:00
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
2021-04-27 11:39:33 +07:00
### VIPER
2021-04-14 04:02:14 +07:00
2021-04-10 04:13:06 +07:00
## UIDesign
2021-04-10 03:58:50 +07:00
2021-04-10 04:16:24 +07:00
### HIG(Human Interface Guidelines)
2021-04-10 14:51:46 +07:00
- [Apple UI Kit ](https://developer.apple.com/documentation/uikit )
2021-05-03 10:58:56 +07:00
- [iOS Design Guide ](https://ivomynttinen.com/blog/ios-design-guidelines )
2021-04-10 04:16:24 +07:00
2021-04-10 04:13:06 +07:00
### iOS icon
2021-04-10 04:12:26 +07:00
2021-04-10 15:45:26 +07:00
- [icon8 ](https://icons8.com/ ) you can download icons for your **APP**
2021-04-10 03:58:50 +07:00
2021-04-10 04:13:06 +07:00
### UIdesign inspiration
2021-04-10 03:58:50 +07:00
2021-04-10 04:33:17 +07:00
- [dribble ](https://dribbble.com/ )
- [pinterest ](https://pinterest.com/ )
- [behance ](https://www.behance.net/ )
- [pttrns ](https://pttrns.com/ )
- [awwwards ](https://www.awwwards.com/ )
- [flickr ](http://www.flickr.com/ )
- [mobbin ](https://mobbin.design/ )
2021-04-22 17:08:43 +07:00
## Helper
2021-04-22 17:06:56 +07:00
2021-05-03 13:57:35 +07:00
All files are resuable files and protocol oriented. **Just Copy and Paste inside your project and use it!!** 👍
### Email, Message, Call
2021-04-22 17:06:56 +07:00
You can check the file in the follow link
2021-04-22 17:08:43 +07:00
- [Email, Message, Call ](https://github.com/jphong1111/Useful_Swift/blob/main/Helper/ConversationManager.swift )
### Usage
2021-04-22 17:14:29 +07:00
import MesaageUI first
```swift
import MessageUI
```
Then use it
2021-04-22 17:12:51 +07:00
```swift
lazy var manager = ConversationManager(presentingController: self, mailDelegate: self, messageDelegate: self )
func sendEmail() -> MFMailComposeViewController {
manager.sendEmail(feedback: MailFeedback(recipients: ["abcd@example.com"], subject: "Sample", body: "Write body"))
}
func sendMessage() -> MFMessageComposeViewController {
manager.sendMessage(feedback: MessageFeedBack(recipients: ["1111111111"], body: "Type here"))
}
func call() {
manager.makeCall(number: "1111111111")
}
```
2021-04-10 03:58:50 +07:00
2021-05-03 13:57:35 +07:00
### Network Layer
2021-05-03 13:58:22 +07:00
- [Network Layer ](https://github.com/jphong1111/Useful_Swift/tree/main/Helper/Network%20Layer )
2021-05-03 13:57:35 +07:00
2021-04-10 04:13:06 +07:00
## API
2021-04-10 04:12:26 +07:00
2021-05-01 09:20:43 +07:00
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.
2021-04-10 04:30:48 +07:00
2021-04-10 15:55:09 +07:00
## JSON
2021-04-10 15:58:08 +07:00
JSON is a language-independent data format
2021-04-10 16:01:31 +07:00
> Which is relative with **KEY - VALUE** pair
2021-04-10 16:30:44 +07:00
```json
2021-04-10 15:58:08 +07:00
{
2021-04-10 16:00:40 +07:00
"main": [
2021-04-10 15:58:08 +07:00
{
2021-04-10 16:00:40 +07:00
"title": "example1",
2021-04-10 16:31:31 +07:00
"body": "body1"
2021-04-10 15:58:08 +07:00
},
{
2021-04-10 16:00:40 +07:00
"title": "example2",
2021-04-10 16:31:31 +07:00
"body: "body2"
2021-04-25 04:53:10 +07:00
}
2021-04-10 15:58:08 +07:00
]
}
2021-04-10 16:00:40 +07:00
```
2021-04-10 15:55:09 +07:00
### JSON parser extension for Chrome
2021-04-10 16:05:27 +07:00
This extension makes JSON more structable
2021-04-10 15:55:09 +07:00
[JSON parser pro ](https://chrome.google.com/webstore/detail/json-viewer-pro/eifflpmocdbdmepbjaopkkhbfmdgijcc ) **FREE** :+1:
2021-04-10 16:15:30 +07:00
### JSON Decoding
2021-05-01 09:20:43 +07:00
To use JSON Decoding in swift, you have to define the model to be Codable or Decodable
```swift
public typealias Codable = Decodable & Encodable
```
> Decodable can only decode the json data. Can't encoded json file!!
```swift
struct User: Codable {
var first_name: String
var last_name: String
var country: String
}
```
2021-04-10 16:15:30 +07:00
### JSONSerialization
2021-05-01 09:20:43 +07:00
### Various API Site
- [rapidAPI ](https://www.rapidapi.com )
2021-04-29 11:42:01 +07:00
## 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.
2021-04-10 16:15:30 +07:00
2021-04-30 13:40:48 +07:00
### DispatchQueue
2021-04-30 14:06:31 +07:00
An object that manages the execution of tasks serially or concurrently on your app's main thread or on a background thread.
2021-04-30 13:40:48 +07:00
#### main
2021-04-30 14:06:31 +07:00
We can say main is a serial queue
2021-04-30 13:40:48 +07:00
#### global()
2021-04-30 14:06:31 +07:00
We can say global is a concurrent queue
2021-04-30 13:40:48 +07:00
### DispatchGroup
### DispatchWorkItem
2021-04-30 17:16:16 +07:00
### 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 ](https://en.wikipedia.org/wiki/Readers%E2%80%93writers_problem ) in your application.
2021-04-30 13:58:38 +07:00
#### How to Use Address Sanitizer
Go to this Option and Click **EDIT SCHEME...** 👈
2021-04-30 14:01:13 +07:00
< img src = "https://github.com/jphong1111/Useful_Swift/blob/main/Images/address_sanitizer.png" >
2021-04-30 17:16:16 +07:00
And then go to **RUN** and check **THREAD SANITIZER** 👈
2021-04-30 13:58:38 +07:00
2021-04-30 17:18:31 +07:00
< img src = "https://github.com/jphong1111/Useful_Swift/blob/main/Images/thread_sanitizer.png" >
2021-04-30 13:40:48 +07:00
2021-04-10 04:30:48 +07:00
## Third Party Library
2021-04-10 15:33:33 +07:00
[This github ](https://github.com/vsouza/awesome-ios ) contains all the popular libraries in Swift:+1:
2021-04-23 14:19:47 +07:00
## Useful Stuff
2021-04-23 14:25:02 +07:00
### Show Preview in UIKit(Build UI with Code Base) 👍 👍 👍 👍 👍
Copy this code and Paste into your controller
2021-04-23 14:19:47 +07:00
```swift
import SwiftUI
struct ViewControllerRepresentable: UIViewControllerRepresentable {
typealias UIViewControllerType = ViewController
func makeUIViewController(context: Context) -> ViewController {
return ViewController()
}
func updateUIViewController(_ uiViewController: ViewController, context: Context) {
}
}
@available (iOS 13.0.0, *)
struct ViewPreview: PreviewProvider {
static var previews: some View {
ViewControllerRepresentable()
}
}
```
2021-04-23 14:25:02 +07:00
Enable canvas option like this
< img src = "https://github.com/jphong1111/Useful_Swift/blob/main/Images/preview%20using%20canvas.png" >
2021-04-23 14:34:10 +07:00
**You are GOOD TO GO** 👏👏👏
2021-04-23 14:38:50 +07:00
2021-04-23 14:39:56 +07:00
< img src = "https://github.com/jphong1111/Useful_Swift/blob/main/Images/preivew_screenShot.png" >
2021-04-24 00:01:19 +07:00
## Write README.md
2021-04-24 00:01:53 +07:00
[This ](https://medium.com/@saumya.ranjan/how-to-write-a-readme-md-file-markdown-file-20cb7cbcd6f ) will help you to write a README.md file more dynamically 👍
2021-05-01 09:22:58 +07:00
## Author
2021-05-01 09:23:20 +07:00
This README.md file is written by **Jungpyo Hong (Dennis)**