Skip to content

Instantly share code, notes, and snippets.

View yoxisem544's full-sized avatar
πŸ₯‘

David Lin yoxisem544

πŸ₯‘
View GitHub Profile
@dynamicMemberLookup
protocol JSONType {
subscript(dynamicMember member: String) -> JSONType? { get set }
}
extension JSONType {
subscript(dynamicMember member: String) -> JSONType? {
get { nil }
set { }
}
subscript<T>(type: T.Type) -> T? {
@pofat
pofat / Dictionary+Extension.swift
Last active December 28, 2019 22:23
String KeyPath accessor of Dictionary
import Foundation
// Time Complexity: O(N), N stands for the level of key path
extension Dictionary where Key == String {
subscript(keyPath keyPath: String) -> Any? {
get {
guard !keyPath.isEmpty else { return nil }
var value: Any? = self
for key in keyPath.components(separatedBy: ".") {
if let node = (result as? [Key: Any])?[key] {
@pofat
pofat / Either.swift
Created November 14, 2019 15:09
Codable Either
enum Either<A, B> {
case left(A)
case right(B)
}
extension Either: Decodable where A: Decodable, B: Decodable {
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if let a = try? container.decode(A.self) {
self = .left(a)
@yoxisem544
yoxisem544 / GenCert.sh
Last active October 2, 2019 11:49
To help you generate p12 file to import to another mac.
#!/bin/bash
read -p "Please enter passphrase to decrypt certifications: " passphrase
outputPath="./output"
mkdir -p ${outputPath}
read -p "Pleas enter password to decrypt output p12 file: " outputPassword
for entry in ./certs/*/*.cer
do
import Moya
import RxSwift
import PromiseKit
import Alamofire
import SwiftyJSON
typealias Method = HTTPMethod
extension String {
var urlEscaped: String {
@AhmedOS
AhmedOS / PushIDGenerator.swift
Last active October 23, 2020 19:57
Swift 4 port of Firebase Push ID generator
// original: https://gist.github.com/mikelehen/3596a30bd69384624c11
class PushIDGenerator {
private init() { }
static private let PUSH_CHARS = Array("-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz")
static private var lastPushTime: UInt64 = 0
static private var lastRandChars = Array<Int>(repeating: 0, count: 12)
static func generate() -> String {
# This file contains the fastlane.tools configuration
# You can find the documentation at https://docs.fastlane.tools
#
# For a list of all available actions, check out
#
# https://docs.fastlane.tools/actions
#
# For a list of all available plugins, check out
#
# https://docs.fastlane.tools/plugins/available-plugins
// UIViewController
import UIKit
final public class <#AnyViewController#>: UIViewController {
// MARK: - πŸ“Œ Constants
// MARK: - πŸ”Ά Properties
// MARK: - 🎨 Style
// MARK: - 🧩 Subviews
// MARK: - πŸ‘† Actions
extension Date {
var secondPassedFromNow: TimeInterval {
return -timeIntervalSince(Date())
}
var relativeDate: String {
// if second is negative, means its in future
let seconds = secondPassedFromNow.rounded(.down)
guard seconds >= 0 else { return "\(seconds)" }
extension Int {
var relativeCount: String {
return relativeCount(toDigit: 1)
}
func relativeCount(toDigit digit: Int) -> String {
// should not round to negative digit, its impossible
guard digit >= 0 else { fatalError() }
// if is negative, just return self