Skip to content

Instantly share code, notes, and snippets.

Avatar
💭
better than last action

Dmytro Pylypenko dimpiax

💭
better than last action
View GitHub Profile
View C-Localized.swift
class NatureViewController: UIViewController {
@Localized(.natureTitle)
@IBOutlet private var label: UILabel!
@Localized(.saveNatureButton)
@IBOutlet private var button: UIButton!
}
View LocalizationKey.swift
enum LocalizationKey: String {
case
natureTitle,
saveNatureButton
}
extension LocalizationKey {
var string: String {
NSLocalizedString(rawValue, comment: rawValue)
}
View Localizable.swift
protocol Localizable {
func set(localization: LocalizationKey)
}
extension UIButton: Localizable {
func set(localization key: LocalizationKey) {
setTitle(key.string, for: .normal)
}
}
View Localized.swift
@propertyWrapper
struct Localized<T: Localizable> {
private let key: LocalizationKey
var wrappedValue: T? = nil {
didSet {
wrappedValue?.set(localization: key)
}
}
View A-Localized.swift
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)
View B-Localized.swift
class NatureViewController: UIViewController {
@Localized("natureTitle")
@IBOutlet private var label: UILabel!
@Localized("saveNatureButton")
@IBOutlet private var button: UIButton!
}
@dimpiax
dimpiax / UIFont+.swift
Last active Oct 9, 2017
UIFont's extension which serves FontDescriptor attributes
View UIFont+.swift
//
// 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 Sep 22, 2017
Swift custom date decoder. ISO8601 without milliseconds
View iso8601.swift
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 Sep 19, 2017
Lite Promise.sync using async/await
View promiseUtils.js
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 Jul 17, 2017
Optional Bool shortcut
View Optional+.swift
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
You can’t perform that action at this time.