Skip to content

Instantly share code, notes, and snippets.

gonzalezreal / KeyedDecodingContainer+EmptyRepresentable.swift
Last active Jul 30, 2021
A technique to avoid having optional array properties in your models when decoding JSON using Swift 4
View KeyedDecodingContainer+EmptyRepresentable.swift
/// A type that has an "empty" representation.
public protocol EmptyRepresentable {
static func empty() -> Self
extension Array: EmptyRepresentable {
public static func empty() -> Array<Element> {
return Array()
khanlou / MD5StringTest.swift
Last active Apr 12, 2019
MD5 and SHA1 on String in Swift 3
View MD5StringTest.swift
import XCTest
@testable import <#project#>
class StringMD5Test: XCTestCase {
func testMD5() {
let string = "soroush khanlou"
XCTAssertEqual(string.md5, "954d741d14b14002d1ba88f600eee635")
View UIScrollView+RACSupport.h
// UIScrollView+RACSupport.h
// Taskworld
// Created by Kittinun Vantasin on 8/5/15.
// Copyright (c) 2015 Taskworld. All rights reserved.
@import UIKit;
hlung / iOS - constants declaration.m
Last active Nov 25, 2015
iOS - how to create constants in private and public scope
View iOS - constants declaration.m
// ----------------------------------
//declaration in PRIVATE scope (for using only in your class)
// ----------------------------------
// declare in .m
static const int kTweetMaxCharacters = 140;
static NSString * const kHelloString = @"HELLO!";
// ----------------------------------
// declaration in PUBLIC scope (for using in other classes too)
// ----------------------------------
View ArrayHelpers.swift
extension Array {
func first() -> Element? {
if isEmpty {
return nil
return self[0]
func last() -> Element? {
tomasbasham / UIImage+Scale.m
Last active May 22, 2022
Scale a UIImage to any given rect keeping the aspect ratio
View UIImage+Scale.m
@implementation UIImage (scale)
* Scales an image to fit within a bounds with a size governed by
* the passed size. Also keeps the aspect ratio.
* Switch MIN to MAX for aspect fill instead of fit.
* @param newSize the size of the bounds the image must fit within.
* @return a new scaled image.
claybridges / gist:6029091
Last active Jul 10, 2018
UIInterfaceOrientationMask vs. UIInterfaceOrientation. As far as I know, a function like this isn't available in the API. I derived this from the enum def for UIInterfaceOrientationMask.
View gist:6029091
// UIInterfaceOrientationMask vs. UIInterfaceOrientation
// As far as I know, a function like this isn't available in the API. I derived this from the enum def for
// UIInterfaceOrientationMask.
inline BOOL OrientationMaskSupportsOrientation(UIInterfaceOrientationMask mask, UIInterfaceOrientation orientation) {
return (mask & (1 << orientation)) != 0;
eddiemoya /
Last active Sep 6, 2018
Flip the last two commits in a branch using git-rebase, git-cherry-pick, git-update-ref, git-name-rev, and git-rev-parse. Interesting exercise using quite a bit of plumbing commands.
branch=$(git name-rev --name-only HEAD)
git rebase --onto HEAD~2 HEAD~1 HEAD
git cherry-pick ORIG_HEAD~1
git update-ref refs/heads/$branch $(git rev-parse HEAD)
git checkout --quiet $branch
# Instead of creating an independant bash script with the code above,
# consider simply creating a git alias using the command below.
statico / gist:3172711
Created Jul 24, 2012
How to use a PS3 controller on Mac OS X 10.7 (Lion)
View gist:3172711

How to use a PS3 controller on Mac OS X 10.7 (Lion)

  1. Open Apple menu -> System Preferences -> Bluetooth and disable Bluetooth on Mac as well as any other nearby Macs or devices which will try to pair with and confuse the controller.

  2. Reset PS3 controller by inserting paperclip into pinhole near L2 button.

  3. Connect PS3 controller to Mac with USB cable.

  4. Enable Bluetooth.

indragiek / NSWindow+Fade.h
Created Nov 27, 2011
NSWindow+Fade - Animator proxy based NSWindow fading
View NSWindow+Fade.h
@interface NSWindow (Fade)
- (IBAction)fadeIn:(id)sender;
- (IBAction)fadeOut:(id)sender;