Skip to content

Instantly share code, notes, and snippets.

View lbrndnr's full-sized avatar

Laurin Brandner lbrndnr

View GitHub Profile
infix operator --> { associativity left precedence 160 }
/// True if and only if lhs implies rhs.
/// False if lhs is true and rhs false, true otherwise.
func -->(lhs: Bool, rhs: Bool) -> Bool {
return !lhs || rhs
}
@lbrndnr
lbrndnr / gist:19fe28bc0f85311fc489
Created May 28, 2015 10:55
Swift confusing generic types
class Cell: UITableViewCell {
func hello() -> String {
return "hello"
}
}
var payload = [Int: AnyObject]()
payload[1] = Cell()
@lbrndnr
lbrndnr / gist:705946cb937fadeebca4
Created April 29, 2015 09:39
UIColor to init them using hex values: UIColor(0x8046A2)
extension UIColor {
convenience init(_ hexValue: Int) {
let r = (hexValue & 0xFF0000) >> 16
let g = (hexValue & 0x00FF00) >> 8
let b = hexValue & 0x0000FF
self.init(red: CGFloat(r)/255, green: CGFloat(g)/255, blue: CGFloat(b)/255, alpha: 1)
}
@lbrndnr
lbrndnr / gist:f54d81079007477744c7
Created February 8, 2015 17:49
Why do nested function not work within closures?
func functionWithClosure(closure: () -> ()) {
closure()
}
func topFunction() {
let button = UIButton()
let image = UIImage()
func nestedFunction() {
button.setImage(image, forState: .Normal)
@lbrndnr
lbrndnr / gist:ea8bded60e723ceb9a2e
Created January 27, 2015 16:01
A generic -objectForKey: (e.g. NSUserDefaults, NSCache)
var dictionary = ["yolo": 9]
func objectForKey<T>(key: String) -> T? {
let obj = dictionary[key]
if let obj = obj as? T {
return obj
}
return nil
@lbrndnr
lbrndnr / gist:3968564
Created October 28, 2012 13:17
Tweetbotish UITableView animation when reloading it
-(void)reloadData {
BOOL canAnimate = !self.visibleCells.count;
[self reloadData];
if (canAnimate) {
CABasicAnimation* revealAnimation = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
revealAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
revealAnimation.duration = 0.3f;
[revealAnimation setToValue:[NSNumber numberWithFloat:0.0f]];
@lbrndnr
lbrndnr / gist:3947009
Created October 24, 2012 16:09
Calling a method if the receiver does respond to it.
-(BOOL)performSelectorIfResponding:(SEL)selector withObjects:(id)firstParameter, ... {
if (![self respondsToSelector:selector]) {
return NO;
}
NSMethodSignature* signature = [self.class instanceMethodSignatureForSelector:selector];
NSInvocation* invocation = [NSInvocation invocationWithMethodSignature:signature];
invocation.target = self;
invocation.selector = selector;
@lbrndnr
lbrndnr / gist:2311820
Created April 5, 2012 15:04
Tumblr dashrboard request
OARequestParameter* likesParameter = [OARequestParameter requestParameter:@"likes" value:@"1"];
NSURL* URL = [NSURL URLWithString:dashboardURL];
__block void (^successBlock)(AFHTTPRequestOperation *operation, id JSON);
__block void (^alternateSuccessBlock)(AFHTTPRequestOperation *operation, id JSON);
if (ID && block) {
__block int offset = 0;
__block NSMutableArray* allPosts = [NSMutableArray array];
@lbrndnr
lbrndnr / gist:1432038
Created December 5, 2011 02:27
Checks whether the receiver contains the specified string.
-(BOOL)containsSubstring:(NSString *)substring {
return ([self rangeOfString:substring].location != NSNotFound);
}
@lbrndnr
lbrndnr / gist:1384568
Created November 22, 2011 01:10
Releasing unused UITableViewCells
-(void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
NSMutableDictionary* cells = [self.tableView valueForKey:@"_reusableTableCells"];
for (UITableViewCell* cell in self.tableView.visibleCells) {
if ([cell.reuseIdentifier isEqualToString:@"Text"]) {
return;
}
}
[cells removeObjectForKey:@"Text"];