Skip to content

Instantly share code, notes, and snippets.

@yHomework
yHomework / Debouncer.swift
Created March 24, 2020 12:24 — forked from Ikloo/Debouncer.swift
Deferred funcs calls for Swift
//
// Debouncer.swift
//
// Created by Kirill Budevich on 11/14/19.
// Copyright © 2019 KB. All rights reserved.
//
import Foundation
final public class Debouncer {
@yHomework
yHomework / FadeView.swift
Created March 24, 2020 12:29 — forked from lpbas/FadeView.swift
Fade a UIView in ios using Gradient (Swift Extension)
///
/// With this extension you can call .fadeView(style: ) on any view to create a fading effect.
/// Very useful when trying to fade a UIScrollView, UITableView or UICollectionView
/// This way also takes care of the "white or black" transparent gradient that happens as described here: https://stackoverflow.com/questions/24882361/ios-white-to-transparent-gradient-layer-is-gray
/// This solution is a swift implementation of the following stackoverflow question in Swift, adding more styles: https://stackoverflow.com/questions/17774761/how-to-create-a-top-fade-effect-using-uiscrollview/25408833#25408833
///
extension UIView {
enum UIViewFadeStyle {
case bottom
@yHomework
yHomework / FormattedTextField.swift
Created May 4, 2020 00:51 — forked from darrarski/FormattedTextField.swift
SwiftUI FormattedTextField - TextField with custom display/edit formatters
import SwiftUI
public struct FormattedTextField<Formatter: TextFieldFormatter>: View {
public init(_ title: String,
value: Binding<Formatter.Value>,
formatter: Formatter) {
self.title = title
self.value = value
self.formatter = formatter
}
@dynamicMemberLookup
struct Template {
var template : String
private var data : [String:String]
var populatedTemplate : String { data.reduce(template) { $0.replacingOccurrences(of: "${\($1.key)}", with: $1.value) } }
init(template: String, data: [String:String] = [:]) {
self.template = template
self.data = data
}
// Created by Marcin Krzyzanowski
import Foundation
public protocol JSONEncodable: Encodable { }
public extension JSONEncodable {
func toJSON(using encoder: @autoclosure () -> JSONEncoder = JSONEncoder()) throws -> String {
try String(decoding: encoder().encode(self), as: UTF8.self)
}
import Foundation
public protocol JSONEncodable: Encodable {
static var keyEncodingStrategy: JSONEncoder.KeyEncodingStrategy { get }
static var dateEncodingStrategy: JSONEncoder.DateEncodingStrategy { get }
static var dataEncodingStrategy: JSONEncoder.DataEncodingStrategy { get }
static var nonConformingFloatEncodingStrategy: JSONEncoder.NonConformingFloatEncodingStrategy { get }
func toJSON() throws -> Data
}
@yHomework
yHomework / NestedDecodable.swift
Created June 15, 2020 13:34 — forked from ilyapuchka/NestedDecodable.swift
Decoding nested values with property wrapper
import Foundation
public struct Unit: Codable, Equatable {
init() {}
public init(from decoder: Decoder) {}
public func encode(to encoder: Encoder) throws {}
public static func ==(lhs: Self, rhs: Self) -> Bool {
return true
}
}
import Foundation
public extension Dictionary where Key == String, Value == Any {
fileprivate func _get<T>(path: [String]) -> T? {
var root = self
for idx in 0 ..< path.count - 1 {
guard let _root = root[path[idx]] as? [String: Any] else {
return nil
}
import SwiftUI
fileprivate struct MenuButtonView: View {
var body: some View {
MenuButton(color: .white)
.frame(width: 20, height: 20, alignment: .center)
}
}
@yHomework
yHomework / Demo.swift
Created September 7, 2020 05:35 — forked from AliSoftware/Demo.swift
NestableCodingKey: Nice way to define nested coding keys for properties
struct Contact: Decodable, CustomStringConvertible {
var id: String
@NestedKey
var firstname: String
@NestedKey
var lastname: String
@NestedKey
var address: String
enum CodingKeys: String, NestableCodingKey {