Skip to content

Instantly share code, notes, and snippets.

@ijoshsmith
ijoshsmith / PreferredViewSizeLookup.m
Created September 26, 2012 04:17
Inspect a View's NIB to determine its preferred/natural size
// This code assumes that ARC is enabled.
// Returns the size of this View in its NIB.
+ (CGSize)preferredSize
{
static NSValue *sizeBox = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// Assumption: The XIB file name matches this UIView subclass name.
UINib *nib = [UINib nibWithNibName:NSStringFromClass(self) bundle:nil];
@ijoshsmith
ijoshsmith / SelfFromBlockInARC
Created September 28, 2012 17:59
Safely access self from block using ARC
__weak id weakSelf = self;
void (^aBlock)(void) = ^{
<MyClass> *strongSelf = weakSelf;
if (strongSelf)
{
// strongSelf->someIvar = 42;
// [strongSelf someMethod];
}
};
@ijoshsmith
ijoshsmith / Array+Shuffle.swift
Last active December 24, 2018 21:55
Randomly shuffle a Swift array
import Foundation
extension Array
{
/** Randomizes the order of an array's elements. */
mutating func shuffle()
{
for _ in 0..<10
{
sort { (_,_) in arc4random() < arc4random() }
@ijoshsmith
ijoshsmith / CrossJoin.swift
Last active January 2, 2019 22:59
Cross-joining two Swift arrays
extension Array
{
func crossJoin<E, R>(
array: [E],
joiner: (t: T, e: E) -> R?)
-> [R]
{
return arrayCrossJoin(self, array, joiner)
}
}
@ijoshsmith
ijoshsmith / arrayToDict.swift
Last active November 1, 2019 05:08
Create Swift Dictionary from Array
/**
Creates a dictionary with an optional
entry for every element in an array.
*/
func toDictionary<E, K, V>(
array: [E],
transformer: (element: E) -> (key: K, value: V)?)
-> Dictionary<K, V>
{
return array.reduce([:]) {
@ijoshsmith
ijoshsmith / marshalMadness.swift
Created July 9, 2014 18:17
Having some fun with the marshal operator in Swift
import UIKit
// The marshal operator is reviewed in this blog post…
// http://ijoshsmith.com/2014/07/05/custom-threading-operator-in-swift/
// Don't ever do something this ridiculous in a real app!
func printMarshalOperator()
{
{p("M")}
@ijoshsmith
ijoshsmith / main.swift
Last active August 29, 2015 14:04
Nil-coalescing Operator in Swift
//
// UPDATE: This gist was rendered obsolete as of
// Xcode 6 Beta 5, which introduced the nil
// coalescing operator in Swift proper (??).
//
/* Nil-coalescing operator */
infix operator !! {}
func !! <T> (
value: T?,
@ijoshsmith
ijoshsmith / main.swift
Last active August 29, 2015 14:04
Simplifying NSJSONSerialization in Swift
import Foundation
/** Outputs of the JSONObjectWithData function. */
enum JSONObjectWithDataResult
{
case Success(AnyObject)
case Failure(NSError)
}
/**
@ijoshsmith
ijoshsmith / childviewcontrollers.m
Created January 14, 2015 16:56
Adding and removing child view controllers
// These methods assume that `self` references a UIViewController instance.
- (void)loadChildViewController:(UIViewController *)viewController
{
[self addChildViewController:viewController];
[self.view addSubview:viewController.view];
[viewController didMoveToParentViewController:self];
}
- (void)unloadChildViewController:(UIViewController *)viewController
@ijoshsmith
ijoshsmith / fizzbuzz.exs
Created February 14, 2015 20:54
Elixir solution to the FizzBuzz test
# Elixir (v1.0.3) solution to the FizzBuzz test, defined as:
#
# Write a program that prints the numbers from 1 to 100. But for multiples of three print
# "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers
# which are multiples of both three and five print "FizzBuzz".
# Source: http://c2.com/cgi/wiki?FizzBuzzTest
#
# Inspired by programming exercises in the book 'Programming Elixir' by Dave Thomas.
defmodule FizzBuzz do