Skip to content

Instantly share code, notes, and snippets.

View ppth0608's full-sized avatar
🍕
Stay hungry

Ben.Park ppth0608

🍕
Stay hungry
View GitHub Profile
@ppth0608
ppth0608 / marker.swift
Created November 26, 2019 02:56
How to implement Marker protocol in swift
import UIKit
protocol Paper {}
class APaper: Paper {
var a: String
var text: String
init(a: String, text: String) {
@ppth0608
ppth0608 / CollectionCellSize.swift
Created July 28, 2019 07:18
How to make square size of collection view cell
func collectionViewSize(flowLayout: UICollectionViewFlowLayout, numberOfItemsInRow count: Int) -> CGSize {
let itemSpacing = flowLayout.minimumInteritemSpacing
let leftInset = flowLayout.sectionInset.left
let rightInset = flowLayout.sectionInset.right
let size = (collectionView.bounds.width - (leftInset + rightInset) - (itemSpacing * CGFloat(count - 1))) / CGFloat(count)
return CGSize(width: size, height: size)
}
@ppth0608
ppth0608 / Reusable.swift
Created July 28, 2019 07:12
How to make `register`, `dequeueReusableCell` functions more flexibly
import UIKit
protocol Reusable { }
extension UITableViewCell: Reusable { }
extension UICollectionViewCell: Reusable { }
extension UITableView {
func register<T: Reusable>(_ type: T.Type) {
@ppth0608
ppth0608 / String+Ben.swift
Created July 28, 2019 07:10
How to make `md5` hashed string.
import Foundation
import CommonCrypto
//Reference : https://stackoverflow.com/questions/55356220/get-string-md5-in-swift-5
extension String {
var md5: String {
let data = Data(self.utf8)
let hash = data.withUnsafeBytes { (bytes: UnsafeRawBufferPointer) -> [UInt8] in
var hash = [UInt8](repeating: 0, count: Int(CC_MD5_DIGEST_LENGTH))
@ppth0608
ppth0608 / UIViewController+Ben.swift
Created July 28, 2019 07:09
How to find `topMostViewController`
import UIKit
extension UIViewController {
class var rootTabBarController: TabBarController? {
guard let keyWindow = UIApplication.shared.keyWindow else { return nil }
return keyWindow.rootViewController as? TabBarController
}
class var topMost: UIViewController? {
@ppth0608
ppth0608 / UIPanGestureRecognizer+Ben.swift
Created July 28, 2019 07:08
How to make `direction` of pan gesture
import UIKit
extension UIPanGestureRecognizer {
enum Direction: String {
case up
case down
case left
case right
@ppth0608
ppth0608 / ISImageView.swift
Created July 28, 2019 07:06
How to add gestureRecognizer like Instagram pinch+pan gesture
import UIKit
public class ISImageView: UIImageView {
@IBInspectable public var isInteractable: Bool = false {
didSet {
guard oldValue != isInteractable else { return }
if isInteractable {
setupGesture()
cellForTarget(superview: superview)?.clipsToBounds = false
protocol URLSchemeConvertible {
var url: URL? { get }
var urlString: String { get }
}
extension URLSchemeConvertible {
var queryItems: [URLQueryItem]? {
return URLComponents(string: urlString)?.queryItems
}
@ppth0608
ppth0608 / ObservableType+Ben.swift
Created July 14, 2019 05:20
How use Notification in RxSwift
extension ObservableType {
func postNotification(_ name: Notification.Name) -> Observable<E> {
return self.do(onNext: { element in
NotificationCenter.default.post(name: name, object: element)
})
}
}
/////////
@ppth0608
ppth0608 / UIAlertController+Ben.swift
Created July 14, 2019 04:22
How to using UIAlertController with RxSwift
import RxSwift
import UIKit
extension UIAlertController {
struct AlertAction {
var title: String?
var style: UIAlertAction.Style
static func action(title: String?, style: UIAlertAction.Style = .default) -> AlertAction {