Skip to content

Instantly share code, notes, and snippets.

View fewlinesofcode's full-sized avatar

Few Lines Of Code fewlinesofcode

View GitHub Profile
@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 {
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1 /*Delay time from now*/) {
// Your needed function
}
@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)"
},
{
import math
def to_deg(angle):
return angle * (180 / math.pi)
def to_rad(angle):
return angle * (math.pi / 180)
# Environment
airDensity = 1.2234 # kg/m^3
@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 / 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 / ClosuresCheatSheetPG.swift
Created October 22, 2018 07:53
Playground contents for "Closures cheatsheet"
import Foundation
/*
> Closures are self-contained blocks of functionality that can be passed around and used in your code. Closures in Swift are similar to blocks in C and Objective-C and to lambdas in other programming languages. Swift’s closure expressions have a clean, clear style, with optimizations that encourage brief, clutter-free syntax in common scenarios.
For better understanding, I strongly recommend you to read the [Swift functions cheatsheet]({% post_url 2016-02-12-swift-functions-cheatsheet %}) and it's sources.
# Closure syntax
Since any function is the special case of closure, they are pretty the same. Basic difference is the way of writing and the use purpose. Closures syntax is optimized to be convenient for *inlining*, *passing as parameter* and *using as return type* of other functions.
The general form of the Swift closure is:
{ (parameters) -> ReturnType in
@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 / 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 / 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 {