Skip to content

Instantly share code, notes, and snippets.

View hollance's full-sized avatar

Matthijs Hollemans hollance

View GitHub Profile
extension String {
subscript(i: Int) -> Character {
return Array(self)[i]
}
}
var s = "Hello world"
println(s[0])
@hollance
hollance / Set.swift
Created June 23, 2014 16:05
Very basic set in Swift
class Set<T: Hashable>: Sequence, Printable {
var dictionary = Dictionary<T, Bool>() // private
func addElement(newElement: T) {
dictionary[newElement] = true
}
func removeElement(element: T) {
dictionary[element] = nil
}
import Foundation
import CoreGraphics
@infix func * (left: CGPoint, right: (a: CGFloat, b: CGFloat)) -> CGPoint {
return CGPoint(x: left.x * right.a, y: left.y * right.b)
}
let pt1 = CGPointMake(100, 50)
let pt2 = pt1 * (2, 3)
@hollance
hollance / PrimeFactorSequence.swift
Last active August 29, 2015 14:05
Solution to Explore Swift Challenge #1
/*
The code from https://gist.github.com/GarthSnyder/c8efe80675cd00322b51
rewritten as a Swift sequence and generator.
*/
import Foundation
struct PrimeFactorGenerator: GeneratorType {
var n: Int
var divisor = 2
@hollance
hollance / TakeWhile.swift
Created August 15, 2014 07:11
Explore Swift Challenge #2
struct TakeWhile<S: SequenceType>: SequenceType {
private let seq: S
private let block: (S.Generator.Element) -> Bool
init(_ seq: S, block: (S.Generator.Element) -> Bool) {
self.seq = seq
self.block = block
}
func generate() -> GeneratorOf<S.Generator.Element> {
var gen = seq.generate()
return GeneratorOf<S.Generator.Element> {
@hollance
hollance / gist:c0e1f84d2932ce39fed8
Created August 22, 2014 17:55
Calculating height for a UILabel
var rect = CGRect(x: 100, y: 10, width: 205, height: 10000)
addressLabel.frame = rect
addressLabel.sizeToFit()
rect.size.height = addressLabel.frame.size.height
addressLabel.frame = rect
//
// NSArray-Blocks.h
// Handy codebits
//
// If you want to keep block definitions terse, simple and dynamic, have no
// problems with the incompatible block pointer types and you don't mind
// compiler warnings about sending a message without matching signature,
// DO NOT IMPORT THIS FILE, seriously.
//
// Created by Sijawusz Pur Rahnama on 15/11/09.
@hollance
hollance / gist:1534156
Created December 29, 2011 13:42
Loading view from nib
@implementation UIView (MHExtensions)
+ (id)mh_viewFromNibNamed:(NSString *)nibName owner:(id)ownerOrNil
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:nibName owner:ownerOrNil options:nil];
for (id object in nib)
{
if ([object isKindOfClass:[self class]])
return object;
}
@hollance
hollance / gist:2211705
Created March 27, 2012 02:01
Using GCD to perform a task in the background
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^
{
// do stuff here
dispatch_async(dispatch_get_main_queue(), ^
{
// do stuff on main thread
});
});
@hollance
hollance / gist:2936287
Created June 15, 2012 12:42
Drawing inner glow with Core Graphics
- (CGImageRef)createMaskFromAlphaChannel:(UIImage *)image
{
size_t width = image.size.width;
size_t height = image.size.height;
NSMutableData *data = [NSMutableData dataWithLength:width*height];
CGContextRef context = CGBitmapContextCreate(
[data mutableBytes], width, height, 8, width, NULL, kCGImageAlphaOnly);