Skip to content

Instantly share code, notes, and snippets.

View fitomad's full-sized avatar
👋
Swift + ML & DL + Azure + AWS

Adolfo fitomad

👋
Swift + ML & DL + Azure + AWS
View GitHub Profile
///
/// La aportación de **Louis Van Gaal**
/// al desarrollo del lenguaje Swift.
///
/// La explicación en este vídeo de [Youtube](https://www.youtube.com/watch?v=Od5PQoRr7hU)
///
extension SignedIntegerType
{
/**
Convierte un número entero en negativo
//
// Swift version 2.2-dev (LLVM 3ebdbb2c7e, Clang f66c5bb67b, Swift 42591f7cba)
// Target: x86_64-unknown-linux-gnu
//
// [Swift IBM Sandbox](http://swiftlang.ng.bluemix.net/#/repl)
//
import Foundation
///
/**
Execute closure after a delay
- Parameters:
-delay: Time to wait until execute closure
- closure: What is going to be executed
*/
public func delay(delay: Double, closure: () -> ())
{
let time_gcd: dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, Int64(delay * Double(NSEC_PER_SEC)))
//
// Las variables `queue` y `timer` deben estar deblaradas
// como variables de la clase para así no perder la
// referencia.
//
// queue es de `tipo dispatch_queue_t`
// timer es de tipo `dispatch_source_t`
//
private func createTimer() -> Void
{
@fitomad
fitomad / private-fileprivate-swift2.swift
Last active August 24, 2016 08:09
Usage of private access modifier in Swift 2
//
// In *Swift 2* this works cause `private` means
// **inside this file**
//
//
// MARK: - A Class
//
internal class FilterView: UIView
//
// In *Swift 3* we have to change `private` in favor of `fileprivate`
// Now `private` means **accesible under the scope**, usually {...} and
// `fileprivate` is accesible inside the file.
//
//
// MARK: - A Class
//
//
// Definiendo el callback como tipo
//
typedef ResultCompletionHandler = (mensaje: String, tipo: String) -> (Void)
//
// Definido en la declaración del método
//
func findString(texto: String, completionHandler: (mensaje: String, tipo: String) -> (Void)) -> Void
{
//
// Antes. Un callback por cada tipo devuelto
//
typedef ResultVenueCompletionHandler = (result: Venue) -> (Void)
typedef ResultHotelCompletionHandler = (result: Hotel) -> (Void)
//
// Ahora. Al poder ser definidos como genéricos con un tipo nos vale
//
typedef ResultCompletionHandler<T> = (T) -> (Void)
//
// Esta definición *no* le gusta al compilador de `Swift 3`
//
typealias ResultCompletionHandler<T, U> = (message: T, tipo: U) -> (Void)
/*
ERROR at line 3, col 44: function types cannot have argument label 'message'; use '_' instead
typealias ResultCompletionHandler<T, U> = (message: T, tipo: U) -> (Void)
ERROR at line 3, col 56: function types cannot have argument label 'tipo'; use '_' instead
/// Callback genérico
typealias ResultCompletionHandler<T, U> = (_ message: T, _ tipo: U) -> (Void)
//
// Los métodos a los que llamaría el usuario...
//
public func findString(url: String, handler: ResultCompletionHandler<String, String>) -> Void
{
// Hacemos cosas chulas...