Skip to content

Instantly share code, notes, and snippets.

View leoniralves's full-sized avatar
:octocat:

Leonir Deolindo leoniralves

:octocat:
View GitHub Profile
@leoniralves
leoniralves / gist:5040643
Created February 26, 2013 18:07
Função para acrescentar caracteres em sequências numéricas utilizando recursividade.
<?php
function substring_format($value='', $local)
{
$substring = substr_replace($value, '-'.(substr($value, strlen($value)-$local, 4)),strlen($value)-$local, 4);
if (substr($substring, 4, 1) != '-') {
return substring_format($substring, $local+5);
po [[UIWindow keyWindow] _autolayoutTrace] // prints layouts ambiguity
po [view constraintsAffectingLayoutForAxis:0] // horizontal
po [view constraintsAffectingLayoutForAxis:1] // vertical
[view hasAmbiguousLayout] // BOOL
[view exerciseAmbiguityInLayout] // visualizing ambiguity
viewController.extendedLayoutIncludesOpaqueBars = NO;
viewController.automaticallyAdjustsScrollViewInsets = NO;
viewController.edgesForExtendedLayout = UIRectEdgeNone;
@leoniralves
leoniralves / README.md
Created May 31, 2016 14:13 — forked from jonathantneal/README.md
Local SSL websites on Mac OSX

Local SSL websites on Mac OSX

These instructions will guide you through the process of setting up local, trusted websites on your own computer.

These instructions are intended to be used on Mac OSX Yosemite.

NOTE: You may substitute the edit command for nano, vim, or whatever the editor of your choice is. Personally, I forward edit to Sublime Text:

alias edit="/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl"
@leoniralves
leoniralves / getPropertiesSwift.swift
Created July 14, 2016 22:25
Get Properties Swift
struct Person {
let name: String
let lastname: String
}
class Reflection {
func printProperties(mirror: _MirrorType) {
for i in 0..<mirror.count {
let (name, childMirror) = mirror[i]
let value = childMirror.value
@leoniralves
leoniralves / CurrencyConverterWrong.swift
Last active May 15, 2020 18:07
Trecho para exemplificar o uso incorreto de métodos em protocolos
import Foundation
protocol ExchangeRate {
var value: Double { get }
}
extension ExchangeRate {
func businessTaxCalculated() -> Double {
return value*(1.5/100)
}
@leoniralves
leoniralves / CurrencyConverterCorrect.swift
Created May 15, 2020 18:08
Exemplo de uma forma mais correta no uso de protocolos e testes
import Foundation
protocol ExchangeProtocol {
var value: Double { get }
}
protocol Taxable {
var value: Double { get }
func calculated(exch: ExchangeProtocol) -> Double
}
@leoniralves
leoniralves / Cacheable.swift
Last active October 27, 2020 12:44
Swift extension for asynchronous image loading
import Foundation
protocol Cacheable {
associatedtype Key
associatedtype Object
func set(key: Key, object: Object)
func get(key: Key) -> Object?
}
import Foundation
/*
"Method swizzling is the process of changing the implementation of an existing selector.
It’s a technique made possible by the fact that method invocations in Objective-C can be
changed at runtime, by changing how selectors are mapped to underlying functions in a
class’s dispatch table."
Source: https://nshipster.com/method-swizzling/
"This is only possible because in Objective-C the method to call when a message is sent
func getDocumentsDirectory() -> URL {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
return paths[0]
}
func writeOnFile(str: String) {
let filename = getDocumentsDirectory().appendingPathComponent("output.txt")
do {
if FileManager.default.fileExists(atPath: filename.path), let data = "\(str)\n".data(using: .utf8) {
@leoniralves
leoniralves / AsyncDispatcher.swift
Last active March 29, 2021 22:15
To inject, mock, spy and stub DispatchQueue (sync/async).
import Foundation
protocol AsyncDispatcher {
func async(
group: DispatchGroup?,
qos: DispatchQoS,
flags: DispatchWorkItemFlags,
execute work: @escaping @convention(block) () -> Void
)
func async(execute work: @escaping @convention(block) () -> Void)