Skip to content

Instantly share code, notes, and snippets.

@ricardopereira
ricardopereira / Objc-Blocks-cheatsheet.m
Last active December 30, 2016 09:11 — forked from twobitlabs/gist:4226365
Blocks cheat sheet
// http://cocoawithlove.com/2009/10/ugly-side-of-blocks-explicit.html has a nice breakdown of the syntax--it helps to think of the ^ as similar to a pointer dereference symbol *
// block typedef:
typedef void(^Block)();
typedef void(^ConditionalBlock)(BOOL);
typedef NSString*(^BlockThatReturnsString)();
typedef NSString*(^ConditionalBlockThatReturnsString)(BOOL);
// block property with typedef:
@ricardopereira
ricardopereira / SpinlockTestTests.swift
Created October 4, 2016 10:20 — forked from steipete/SpinlockTestTests.swift
Updated for Xcode 8, Swift 3; added os_unfair_lock
//
// SpinlockTestTests.swift
// SpinlockTestTests
//
// Created by Peter Steinberger on 04/10/2016.
// Copyright © 2016 PSPDFKit GmbH. All rights reserved.
//
import XCTest
@ricardopereira
ricardopereira / Animated-rootViewController-transition.m
Last active December 30, 2016 09:04 — forked from gimenete/gist:53704124583b5df3b407
Animated rootViewController transition
// put this in your AppDelegate
- (void)changeRootViewController:(UIViewController*)viewController {
if (!self.window.rootViewController) {
self.window.rootViewController = viewController;
return;
}
UIView *snapShot = [self.window snapshotViewAfterScreenUpdates:YES];
[viewController.view addSubview:snapShot];
self.window.rootViewController = viewController;
@interface MyStringProxy : NSProxy
@property (nonatomic) NSString *target;
@end
@implementation MyStringProxy
- (BOOL)respondsToSelector:(SEL)aSelector {
@ricardopereira
ricardopereira / DataService.swift
Created January 4, 2017 16:26 — forked from marinbenc/DataService.swift
Automatically create simple mocks for protocols in Swift
import Foundation
/// Protocols confoming to this protocol will have a mock generated for them.
protocol AutoMockable {}
protocol DataServiceProtocol: AutoMockable {
func fetchResource(fromURL url: URL, onComplete: (Result)-> Void)
}
@ricardopereira
ricardopereira / StoryboardBackedViewController.h
Created January 20, 2017 14:07 — forked from JaviSoto/SampleViewController.swift
Init based Storyboard View Controller Instantiation
//
// StoryboardBackedViewController.h
// Fabric
//
// Created by Javier Soto on 9/6/15.
// Copyright © 2015 Fabric. All rights reserved.
//
#import <UIKit/UIKit.h>
@ricardopereira
ricardopereira / UITableView_tableHeaderView+AutoLayout.m
Last active February 17, 2017 08:59
Variable-height UITableView tableHeaderView with autolayout
// in a UITableViewController (or any other view controller with a UITableView)
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width, 0)];
header.translatesAutoresizingMaskIntoConstraints = NO;
// [add subviews and their constraints to header]
NSLayoutConstraint *headerWidthConstraint = [NSLayoutConstraint
@ricardopereira
ricardopereira / .vimrc
Last active February 21, 2017 16:07 — forked from bendc/.vimrc
Vim config (`vim +PluginInstall +qall`)
set nocompatible
set encoding=utf-8 nobomb
filetype off
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" Vundle
Plugin 'VundleVim/Vundle.vim'
@ricardopereira
ricardopereira / AsyncBlockOperation.swift
Last active May 24, 2017 16:50 — forked from tomkowz/AsyncBlockOperation.swift
Asynchronous NSBlockOperation
import Foundation
class AsyncBlockOperation: NSOperation {
typealias Block = (completion: () -> Void) -> Void
private lazy var executingAccessQueue = dispatch_queue_create("eu.ricardopereira.AsyncBlockOperation.executing", DISPATCH_QUEUE_CONCURRENT)
private var _executing = false
private lazy var finishedAccessQueue = dispatch_queue_create("eu.ricardopereira.AsyncBlockOperation.finished", DISPATCH_QUEUE_CONCURRENT)
private var _finished = false
// Similar to defer in Swift
#define pspdf_defer_block_name_with_prefix(prefix, suffix) prefix ## suffix
#define pspdf_defer_block_name(suffix) pspdf_defer_block_name_with_prefix(pspdf_defer_, suffix)
#define pspdf_defer __strong void(^pspdf_defer_block_name(__LINE__))(void) __attribute__((cleanup(pspdf_defer_cleanup_block), unused)) = ^
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-function"
static void pspdf_defer_cleanup_block(__strong void(^*block)(void)) {
(*block)();
}
#pragma clang diagnostic pop