Skip to content

Instantly share code, notes, and snippets.

Avatar

はるふ ha1f

View GitHub Profile
View Realm+asDictionary.swift
import Foundation
import RealmSwift
private extension Object {
/// 再帰的にDictionary型に変換する
func asDictionary() -> [String: Any] {
return objectSchema.properties
.reduce(into: [String: Any](), { (dictionary, property) in
let value = self[property.name]
dictionary[property.name] = _serializableValue(value)
View CGPoint+Extension.swift
extension CGPoint {
func map(_ transform: (CGFloat) -> CGFloat) -> CGPoint {
CGPoint(x: transform(x), y: transform(y))
}
}
View UpdatableArray.swift
import Foundation
import RxSwift
import RxRelay
final class UpdatableArray<T>: ObservableType {
enum Action {
case replace([T])
case append([T])
case filter((T) -> Bool)
View Trie.swift
extension Trie {
class Node {
var element: Element?
var children: [Path: Node]
init() {
self.element = nil
self.children = [:]
}
@ha1f
ha1f / PrintEncodable.swift
Created Apr 17, 2020
Print encodable as JSON
View PrintEncodable.swift
private func _jsonSerialization(from encodable: Encodable) throws -> String {
let jsonEncoder = JSONEncoder()
jsonEncoder.outputFormatting = .prettyPrinted
let encodedData = try jsonEncoder.encode(config)
return String(data: encodedData, encoding: .utf8)!
}
func prettyPrint(_ encodable: Encodable) {
do {
print(try _jsonSerialization(from: encodable))
View swiftgen-template-UIImage-extension.stencil
// swiftlint:disable all
// Generated using SwiftGen — https://github.com/SwiftGen/SwiftGen
{% if catalogs %}
{% set imageAlias %}{{param.imageAliasName|default:"AssetImageTypeAlias"}}{% endset %}
{% set accessModifier %}{% if param.publicAccess %}public{% else %}internal{% endif %}{% endset %}
#if os(OSX)
import AppKit.NSImage
{{accessModifier}} typealias {{imageAlias}} = NSImage
#elseif os(iOS) || os(tvOS) || os(watchOS)
View pre-commit.sh
#!/bin/sh
echo "Running pre-commit..."
# pre-commit自体が最新かどうかをチェック
PRECOMMIT_MASTER_FILE='./script/pre-commit'
if [ -e $PRECOMMIT_MASTER_FILE ]; then
diff -s $PRECOMMIT_MASTER_FILE ./.git/hooks/pre-commit > /dev/null 2>&1
if [ $? -ne 0 ]; then
cp $PRECOMMIT_MASTER_FILE ./.git/hooks/pre-commit
View CachableRequest-2.swift
enum CachableRequestState {
/// 読み込み前
case possible
/// 読込中
case loading
/// 直近のリクエストがエラー
case error(Error)
View CachableRequest.swift
//
// CachableRequest.swift
// LIPS
//
import Foundation
import RxSwift
import RxCocoa
private extension ObservableType {
View LazyBehaviorRelay.swift
final class LazyBehaviorRelay<Element>: ObservableType, ObserverType {
private let _behaviorRelay = BehaviorRelay<Element?>(value: nil)
private let _emittedValue: Observable<Element>
init() {
_emittedValue = _behaviorRelay
.filterNil()
.share(replay: 1, scope: .forever)
}