Skip to content

Instantly share code, notes, and snippets.

View d-date's full-sized avatar
🏠
Working from home

Daiki Matsudate d-date

🏠
Working from home
View GitHub Profile
import UIKit
import WebKit
class WKWebViewController: UIViewController {
var request: URLRequest?
weak var uiDelegate: WKUIDelegate?
weak var navigationDelegate: WKNavigationDelegate?
lazy var webView: WKWebView = {
import UIKit
typealias Constraint = (UIView, UIView) -> NSLayoutConstraint
func equal<L, Axis>(_ to: KeyPath<UIView, L>, constant: CGFloat = 0, priority: UILayoutPriority? = nil) -> Constraint where L: NSLayoutAnchor<Axis> {
return equal(to, to, constant: constant, priority: priority)
}
func equal<L, Axis>(_ from: KeyPath<UIView, L>, _ to: KeyPath<UIView, L>, constant: CGFloat = 0, priority: UILayoutPriority? = nil) -> Constraint where L: NSLayoutAnchor<Axis> {
return { view1, view2 in
@d-date
d-date / Podfile
Created April 11, 2019 07:46
Specify swift version for each targets individually
pre_install do |installer|
installer.pod_targets.each do |target|
if ['Mockingjay', 'URITemplate'].include? target.name
target.root_spec.swift_version = '4.2'
else
target.root_spec.swift_version = '5.0'
end
end
end
var directory: ObjCBool = false
var exists: Bool = FileManager.default.fileExists(atPath: "…", isDirectory: &directory)
if exists {
if directory.boolValue {
// Exists. Directory.
} else {
// Exists.
}
} else {
@d-date
d-date / FirebaseInstance.swift
Last active January 23, 2019 06:24
Preprocessor Token for Firebase to avoid initialize Firebase multi-times.
public struct FirebasePreprocessorToken {
public static let `default` = FirebasePreprocessorToken()
private init() {
#if RELEASE
let googleServiceInfoPlist = "GoogleService-Info"
#elseif STAGING
let googleServiceInfoPlist = "GoogleService-Info-Stg"
#else
let googleServiceInfoPlist = "GoogleService-Info-Dev"
@d-date
d-date / CodePiece.swift
Created January 4, 2019 11:03
DateIntervalで遊んだ(友達待ってる間の暇つぶし) #CodePiece
import Foundation
extension Date {
var elasticDateFormat: String {
let calendar = Calendar.current
let date = Date()
let dateInterval = calendar.dateInterval(of: .month, for: self)
if calendar.dateInterval(of: .day, for: self)!.contains(date) {
return "HH:mm"
struct Hoge: Storagable {
let id: Int
let name: String
}
protocol Storagable: Codable {}
extension Storagable {
func save(key: String = "\(Self.self)", userDefaults: UserDefaults = .standard) throws {
let data = try JSONEncoder().encode(self)
@d-date
d-date / CodePiece.swift
Created September 3, 2018 21:26
We can apply style like this. Very simple implementation! #CodePiece #tryswiftnyc
let labelColorStyle = { set(\UILabel.textColor, $0) }
let primaryLabelStyle = labelColorStyle(UIColor.green)
let label = UILabel() |> primaryLabelStyle
@d-date
d-date / CodePiece.swift
Created September 3, 2018 21:17
Also, we can user function composition to UI implementation #CodePiece #tryswiftnyc
import UIKit
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
NSMutableParagraphStyle() |> (prop(\.alignment)) { _ in .center }
func set<Root, Value>(
_ kp: WritableKeyPath<Root, Value>,
_ value: Value
)
@d-date
d-date / CodePiece.swift
Created September 3, 2018 21:16
When define prop as this, we can mutate property with function composition #CodePiece #tryswiftnyc
func prop<Root, Value>(
_ kp: WritableKeyPath<Root, Value>
)
-> (@escaping (Value) -> Value)
-> (Root) -> Root {
return { transformPart in
return { root in
var root = root
root[keyPath: kp] = transformPart(root[keyPath: kp])
return root