Skip to content

Instantly share code, notes, and snippets.

View dimpiax's full-sized avatar
💭
better than last action

Dmytro Pylypenko dimpiax

💭
better than last action
  • Barcelona, Spain
View GitHub Profile
class NatureViewController: UIViewController {
@Localized(.natureTitle)
@IBOutlet private var label: UILabel!
@Localized(.saveNatureButton)
@IBOutlet private var button: UIButton!
}
enum LocalizationKey: String {
case
natureTitle,
saveNatureButton
}
extension LocalizationKey {
var string: String {
NSLocalizedString(rawValue, comment: rawValue)
}
protocol Localizable {
func set(localization: LocalizationKey)
}
extension UIButton: Localizable {
func set(localization key: LocalizationKey) {
setTitle(key.string, for: .normal)
}
}
@propertyWrapper
struct Localized<T: Localizable> {
private let key: LocalizationKey
var wrappedValue: T? = nil {
didSet {
wrappedValue?.set(localization: key)
}
}
class NatureViewController: UIViewController {
@IBOutlet private var label: UILabel! {
didSet {
label.title = NSLocalizedString("natureTitle", comment: "")
}
}
@IBOutlet private var button: UIButton! {
didSet {
button.setTitle(NSLocalizedString("saveNatureButton", comment: ""), for: .normal)
class NatureViewController: UIViewController {
@Localized("natureTitle")
@IBOutlet private var label: UILabel!
@Localized("saveNatureButton")
@IBOutlet private var button: UIButton!
}
@dimpiax
dimpiax / UIFont+.swift
Last active October 9, 2017 10:59
UIFont's extension which serves FontDescriptor attributes
//
// UIFont+.swift
// Autofriend
//
// Created by Pilipenko Dima on 10/9/17.
// Copyright © 2017 dimpiax. All rights reserved.
//
import Foundation
import UIKit
@dimpiax
dimpiax / iso8601.swift
Last active September 22, 2017 12:47
Swift custom date decoder. ISO8601 without milliseconds
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .custom { decoder -> Date in
let valueContainer = try decoder.singleValueContainer()
let value = try valueContainer.decode(String.self)
if let regex = try? NSRegularExpression(pattern: "\\.\\d+", options: []) {
let result = regex.stringByReplacingMatches(in: value, options: [], range: NSRange(location: 0, length: value.count), withTemplate: "")
if let date = ISO8601DateFormatter().date(from: result) {
return date
@dimpiax
dimpiax / promiseUtils.js
Last active September 19, 2017 16:23
Lite Promise.sync using async/await
const next = async (it, callback) => {
const { value, done } = it.next()
if (done) return []
const result = await callback(value)
const nextResult = await next(it, callback)
return [result, ...nextResult]
}
const sync = async (value, callback) => {
@dimpiax
dimpiax / Optional+.swift
Created July 17, 2017 09:05
Optional Bool shortcut
extension Optional where Wrapped == Bool {
var bool: Bool {
switch self {
case .some(let value): return value == true
case .none: return false
}
}
}
var foo: Bool? = true