Skip to content

Instantly share code, notes, and snippets.

View olivaresf's full-sized avatar

Fernando olivaresf

View GitHub Profile
@olivaresf
olivaresf / KeychainItemWrapper.h
Last active September 7, 2015 00:52 — forked from yvbeek/KeychainItemWrapper.h
KeychainItemWrapper ARCified
/*
File: KeychainItemWrapper.h
Abstract:
Objective-C wrapper for accessing a single keychain item.
Version: 1.2 - ARCified
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
Inc. ("Apple") in consideration of your agreement to the following
terms, and your use, installation, modification or redistribution of
@olivaresf
olivaresf / gist:9a20b29101a0e898a7af
Created January 6, 2016 04:14
Two-closure syntax sucks
APIManager.signInUser("fer", password: "fer", successBlock: { (user) -> () in
print(user)
}) {
failureBlock: { error in
print(error)
}
@olivaresf
olivaresf / gist:8d34efeb3bc2e6d6278c
Last active January 8, 2016 03:03
An easy way to fix your two-closure Swift indentation
// A beautiful two-closure method.
signInUser("fer", pass: "quetzalmx") { User in
// Success
print(User)
}.onFailure { error in
// Failure
print(error)
}
// Calls a function that returns an object which can optionally fail.
@olivaresf
olivaresf / Enums.swift
Last active November 6, 2019 02:22
An example on how enums can clean up your code
//
// ViewController.swift
// LambdaEnums
//
// Created by Fernando Olivares on 11/1/19.
// Copyright © 2019 Fernando Olivares. All rights reserved.
//
import UIKit
extension UIImageView {
/// Loads image from web asynchronosly and caches it, in case you have to load url
/// again, it will be loaded from cache if available
func load(url: URL, placeholder: UIImage?, cache: URLCache? = nil) {
let cache = cache ?? URLCache.shared
let request = URLRequest(url: url)
if let data = cache.cachedResponse(for: request)?.data, let image = UIImage(data: data) {
self.image = image
} else {
self.image = placeholder
@olivaresf
olivaresf / prepare-commit-msg.sh
Created April 28, 2020 23:06 — forked from bartoszmajsak/prepare-commit-msg.sh
How to automatically prepend git commit with a branch name
#!/bin/bash
# This way you can customize which branches should be skipped when
# prepending commit message.
if [ -z "$BRANCHES_TO_SKIP" ]; then
BRANCHES_TO_SKIP=(master develop test)
fi
BRANCH_NAME=$(git symbolic-ref --short HEAD)
BRANCH_NAME="${BRANCH_NAME##*/}"
@olivaresf
olivaresf / 17072020-original.swift
Last active July 17, 2020 07:27
Example July 17
func requestAuthorization(completion: ((Bool, Error?) -> Void)? = nil) {
// enable local notifications (alarms in background)
let authorizationOptions: UNAuthorizationOptions
if #available(iOS 12.0, *) {
authorizationOptions = [.badge, .alert, .sound, .criticalAlert]
} else {
authorizationOptions = [.badge, .alert, .sound]
}
func requestAuthorization(completion: @escaping (Result<Bool, Error>) -> Void) {
var authorizationOptions: UNAuthorizationOptions = [.badge, .alert, .sound]
if #available(iOS 12.0, *) {
authorizationOptions.insert(.criticalAlert)
}
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.requestAuthorization(options: authorizationOptions) { (granted, possibleError) in
protocol CurrencyPickerProtocol : class {
func selected(currency: String, from: CurrencyPickerTableViewController)
}
class CurrencyPickerTableViewController: UITableViewController {
weak var delegate: CurrencyPickerProtocol
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let key = order[indexPath.row]
// Behavior 1: Save to preferences.
class CurrencyPickerSettingsResponsible { }
extension CurrencyPickerSettingsResponsible : CurrencyPickerProtocol {
func selected(currency: String, from: CurrencyPickerTableViewController) {
let preferences = BTCPreferences.sharedPreferences()
preferences.setObject(key, forKey: kBTCSelectedCurrencyKey)
preferences.synchronize()
NSNotificationCenter.defaultCenter().postNotificationName(kBTCCurrencyDidChangeNotificationName, object: key)