This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__weak id weakSelf = self; | |
void (^aBlock)(void) = ^{ | |
<MyClass> *strongSelf = weakSelf; | |
if (strongSelf) | |
{ | |
// strongSelf->someIvar = 42; | |
// [strongSelf someMethod]; | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
extension Array | |
{ | |
/** Randomizes the order of an array's elements. */ | |
mutating func shuffle() | |
{ | |
for _ in 0..<10 | |
{ | |
sort { (_,_) in arc4random() < arc4random() } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Array | |
{ | |
func crossJoin<E, R>( | |
array: [E], | |
joiner: (t: T, e: E) -> R?) | |
-> [R] | |
{ | |
return arrayCrossJoin(self, array, joiner) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
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([:]) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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?, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
/** Outputs of the JSONObjectWithData function. */ | |
enum JSONObjectWithDataResult | |
{ | |
case Success(AnyObject) | |
case Failure(NSError) | |
} | |
/** |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
OlderNewer