This file contains 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 String { | |
subscript(i: Int) -> Character { | |
return Array(self)[i] | |
} | |
} | |
var s = "Hello world" | |
println(s[0]) |
This file contains 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
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 | |
} |
This file contains 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 | |
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) |
This file contains 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
/* | |
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 |
This file contains 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
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> { |
This file contains 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
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 |
This file contains 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
// | |
// 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. |
This file contains 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
@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; | |
} |
This file contains 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
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 | |
}); | |
}); |
This file contains 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
- (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); |
OlderNewer