Skip to content

Instantly share code, notes, and snippets.

View koingdev's full-sized avatar
🤖
Stay focus

Saingkoing SEA koingdev

🤖
Stay focus
  • Tokyo, Japan
View GitHub Profile
@koingdev
koingdev / GenerateQRFromString.swift
Last active March 12, 2019 14:29
Swift generate a QR Code from text
import UIKit
import Foundation
func generateQR(from string: String) -> Data? {
let data = string.data(using: String.Encoding.utf8)
guard let qrFilter = CIFilter(name: "CIQRCodeGenerator") else { return nil }
qrFilter.setValue(data, forKey: "inputMessage")
@koingdev
koingdev / Usage.swift
Created April 12, 2019 16:57
Swift way to create an autoweak closure (No more retain cycle/memory leak)
// From this
didFinishLoading = { [weak self] in
guard let self = self else { return }
self.tableView.reloadData()
}
// To this
didFinishLoading = weaker(self) { s in
// No more boilerplate code :)
s.tableView.reloadData()
@koingdev
koingdev / UIControlEventHandler.swift
Created May 1, 2019 14:32
Handle UIControl event closure-base instead of (target/action)
/** Handle UIControl event closure-base instead of (target/action)
Example:
```swift
// Button
button.on(.touchUpInside) { btn in
print("Button clicked")
}
// TextField
textField.on(.editingChanged) { tf in
@koingdev
koingdev / UIComponentStyler.swift
Created May 1, 2019 14:35
Flexible and faster way to style your View
/**
Flexible and faster way to style your View
Example:
```swift
let label = UILabel()
label.styling(UIColor.blue.style, UIFont.boldSystemFont(ofSize: 18).style)
```
*/
protocol UIComponentStyler: AnyObject {
@koingdev
koingdev / Debounce.swift
Created May 1, 2019 14:39
Swift Debouncer
/// Return a new function that will be called only once after `delay` time passed between invocation
func debounce(delay: TimeInterval, queue: DispatchQueue = .main, function: @escaping () -> Void) -> () -> Void {
var currentWorkItem: DispatchWorkItem?
return {
currentWorkItem?.cancel()
currentWorkItem = DispatchWorkItem { function() }
queue.asyncAfter(deadline: .now() + delay, execute: currentWorkItem!)
}
}
@koingdev
koingdev / AutoLayoutBuilder.swift
Created May 1, 2019 14:41
Swift Builder Pattern way to write AutoLayout code
/**
Builder Pattern way to write AutoLayout code
Example:
```swift
AutoLayoutBuilder(button)
.bottomTo(view.layoutMarginsGuide.bottomAnchor)
.centerXTo(view.centerXAnchor)
.width(value: button.width)
.height(value: button.height)
@koingdev
koingdev / Log.swift
Created May 1, 2019 14:42
Simple Swift Logger
func dPrint(_ item: Any) {
#if DEBUG
Swift.print(item)
#endif
}
final class Log {
private enum LogType: String {
case error = "[⛔️]"
@koingdev
koingdev / NetworkListener.swift
Created May 1, 2019 14:46
Network Listener (Detect network status changed and check Internet connection)
import Reachability
final class NetworkListener {
private let notification = Notification.Name("NetworkListenerStatusDidChanged")
private let reachability: Reachability!
/// Return true if connected to Internet
private(set) var isConnected: Bool!
init() {
@koingdev
koingdev / UIAlertControllerX.swift
Last active October 27, 2022 06:46
Easy way to show UIAlertController with Builder Pattern
extension UIAlertController {
// MARK: - Typealias
typealias Alert = UIAlertController
typealias Action = UIAlertAction
typealias Block = ((Alert) -> Void)?
// MARK: - Static function
@koingdev
koingdev / OptionalX.swift
Created May 1, 2019 15:07
Optional extension
extension Optional where Wrapped == String {
var orEmpty: Wrapped {
return self ?? ""
}
var isNullOrEmpty: Bool {
return self?.isEmpty ?? true
}