Skip to content

Instantly share code, notes, and snippets.

View fewlinesofcode's full-sized avatar
🏠
Working from home

Oleksandr Glagoliev fewlinesofcode

🏠
Working from home
View GitHub Profile
@fewlinesofcode
fewlinesofcode / TypeErasure.swift
Created November 9, 2018 17:11
Playground code for the article
import Foundation
protocol CustomProtocol {
associatedtype AssociatedType
func foo(argument: AssociatedType)
}
//let array = [CustomProtocol]() // Gives error: Protocol 'CustomProtocol' can only be used as a generic constraint because it has Self or associated type requirements
public struct AnyCustomProtocol<T>: CustomProtocol {
@fewlinesofcode
fewlinesofcode / PlaceholderTextView.swift
Last active November 6, 2018 14:52
The idea was to make the simplest possible solution which allows to use placeholders of different colors, resizes to placeholders size, will not overwrite a `delegate` meanwhile keeping all `UITextView` functions work as expected.
//
// Created by Oleksandr Glagoliev on 05/11/2018.
// Copyright © 2018 Oleksandr Glagoliev. All rights reserved.
//
import UIKit
class PlaceholderTextView: UITextView {
var placeholderColor: UIColor = .lightGray
var defaultTextColor: UIColor = .black
@fewlinesofcode
fewlinesofcode / Playground content
Created October 23, 2018 12:32
IteratorAndSequence.swift
//: Playground - noun: a place where people can play
import UIKit
/*:
## `Date` wrapping and convenience methods
*/
let calendar = Calendar.current
extension Int {
public var days: DateComponents {
@fewlinesofcode
fewlinesofcode / CGSize+AspectCorrectResizing.swift
Created September 24, 2018 10:15
Aspect correct resizing
extension CGSize {
enum AspectMode {
case fit
case fill
}
enum Orientation {
case portrait
case landscape
}
@fewlinesofcode
fewlinesofcode / LoadFromNib.swift
Created July 16, 2018 05:29
Load UIView from NIB
class <#ClassName#>: UIView {
// Do not forget to wire to XIB
@IBOutlet var view: UIView!
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
loadFromNib()
}
init() {
@fewlinesofcode
fewlinesofcode / locales.json
Created April 11, 2018 21:38
i18n locale identifiers in JSON format
[
{
"id": "af",
"description": "Afrikaans"
},
{
"id": "af-NA",
"description": "Afrikaans (Namibia)"
},
{
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1 /*Delay time from now*/) {
// Your needed function
}
@fewlinesofcode
fewlinesofcode / Bounded.swift
Last active January 25, 2017 12:41
Swift implementation of Haskell `Bounded` type
/// Sometimes we need to limit values
/// For instance, you are making application for the Tour De France, and
/// You have a variable depicting the Climb category (1..5)
/// Here is the place, where `Bounded` can be used
struct Bounded<T: Comparable> {
var minimum: T
var maximum: T
var value: T? {
willSet {